[Q] How convert __gc *to DWORD

  • Thread starter ±èº´¼®\( Kevin Kim \)
  • Start date
±

±èº´¼®\( Kevin Kim \)

Hi all

__gc class Test
{
};

void Temp( void )
{
Test *pTest = new Test;

DWORD dwWord = (DWORD)pTest;
}

it code happend error;

Does anyone know how to convert a Test __gc* to an DWORD?

Thanks,

Kevin Kim
 
S

Stoyan Damov

You don't.

This is a managed pointer, you have to pin it.
If you don't pin it, and the compiler let you get the address, later, when
the GC eventually move the object somewhere else in the managed heap, when
you later convert your DWORD to a managed pointer, you will crash.

HTH,
Stoyan Damov
 
F

Filip Konvicka

Hi,

try something more complicated like
DWORD dwWord = *((DWORD*)((void*)&pTest));
But remember that pointers might be 64-bit on future Windows ;-)

If you want to print the pointer value to stdout, use
printf("pTest=%p\n", pTest);

Best Regards,
Filip
 
S

Stoyan Damov

Yeah, sure:))))))))
and when pTest goes out of scope, you are dead:)

Cheers,
Stoyan
 
F

Filip Konvicka

I don't get it. I assume you use unmanaged pure C++. If you do, what I wrote
is correct. If you don't, then I don't see what you want to do and you
should be more specific.

Regards,
Filip
 
F

Filip Konvicka

Umm, sorry, I thought it was Kevin.
Anyway, seems like I missed the __gc prefix. Guess it stands for garbage
collection, and that's none of my bussiness :) (I'm an old C++ guy, you
know.) Sorry.

Filip
 
J

Jochen Kalmbach

Filip said:
I don't get it. I assume you use unmanaged pure C++. If you do, what I
wrote is correct.

But as you could see the example are with MANAGED C++ !!!!
So it will not work!!!!!!

Because the GC (Garbage collector) will move the object to an other
location during execution!!!


Use the gcroot<object*> pattern for unmanaged references to managed
objects!!!


--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top