[Q] How convert __gc *to DWORD

  • Thread starter Thread starter ±èº´¼®\( Kevin Kim \)
  • Start date 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
 
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
 
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
 
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
 
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
 
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
 
Back
Top