473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[Q] How convert __gc *to DWORD

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
Nov 16 '05 #1
7 1927
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

"±èº´¼®( Kevin Kim )" <by*****@msmvp.com.nospam> wrote in message
news:bm**********@news1.kornet.net...
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

Nov 16 '05 #2
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

"±èº´¼®( Kevin Kim )" <by*****@msmvp.com.nospam> wrote in
news:bm**********@news1.kornet.net...
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

Nov 16 '05 #3
±èº´¼®( Kevin Kim ) wrote:
Test *pTest = new Test;

DWORD dwWord = (DWORD)pTest;


Maybe you should explain what you really want to do....
You cannot convert a (managed) class to an DWORD....

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Nov 16 '05 #4
Yeah, sure:))))))))
and when pTest goes out of scope, you are dead:)

Cheers,
Stoyan

"Filip Konvicka" <filip.konvicka at {I.HATE.SPAM} seznam dot cz> wrote in
message news:eB***************@tk2msftngp13.phx.gbl...
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

"±èº´¼®( Kevin Kim )" <by*****@msmvp.com.nospam> wrote in
news:bm**********@news1.kornet.net...
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


Nov 16 '05 #5
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
Yeah, sure:))))))))
and when pTest goes out of scope, you are dead:)

Cheers,
Stoyan

"Filip Konvicka" <filip.konvicka at {I.HATE.SPAM} seznam dot cz> wrote in
message news:eB***************@tk2msftngp13.phx.gbl...
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

"±èº´¼®( Kevin Kim )" <by*****@msmvp.com.nospam> wrote in
news:bm**********@news1.kornet.net...
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



Nov 16 '05 #6
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
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
Yeah, sure:))))))))
and when pTest goes out of scope, you are dead:)

Cheers,
Stoyan

"Filip Konvicka" <filip.konvicka at {I.HATE.SPAM} seznam dot cz> wrote in message news:eB***************@tk2msftngp13.phx.gbl...
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

"±èº´¼®( Kevin Kim )" <by*****@msmvp.com.nospam> wrote in
news:bm**********@news1.kornet.net...
> 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
>
>



Nov 16 '05 #7
Filip Konvicka wrote:
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
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lamberti Fabrizio | last post by:
Hi all. I've got an ActiveX Object that gives me OleColor codes for its graphical components. I need to convert these OleColor codes into RGB codes using JScript or VBScript, becuase I have...
12
by: DanSteph | last post by:
hello all, I have a problem, I have a DOUBLE value like that 4065 4000 0000 0000 and I want to convert it to a DWORD like that: 4065 4000 if I do result=(DWORD)MyDouble it end with...
6
by: Zürcher See | last post by:
Who can tell me how to convert the InternetSetOption in c#? #include "Wininet.h" #pragma comment(lib, "Wininet.lib") static BOOL SetRegisterInfo(HKEY Key, LPCTSTR SubKey, LPCTSTR find,...
3
by: James | last post by:
Has anyone written a utility to convert a C# form to C++.net? i.e. to convert "using System.Data" to "using namespace System::Data" etc
1
by: Ian Lazarus | last post by:
Hello, I tried to confirm that two different heap allocation calls were being made, i.e., one for __gc and one for __nogc. However, the addresses of the calls (as seen in disassembly) are not...
8
by: Boni | last post by:
Please help C# how to convert this C# to MC++ public void Execute(object application, int hwndOwner, ref object contextParams, ref object customParams, ref EnvDTE.wizardResult retval) ; I tried...
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
4
by: viks | last post by:
Hi guys I need little help here . I want to convert 'System::Object __gc * array ' to 'float array' Lets say I have object Reader with method Send ,it returns a variant that contains a...
0
by: Madhu_TN | last post by:
Hi All, I am new to this board. I am trying to create a Crystal Report viewer into a VS C++ Dot NET 2003 app ( This uses both managed and unmanaged code). I get the following compilation error:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.