Getting an HWND

Q

quat

Hello all,

I have some unmanaged code that requires an HWND to a managed control (e.g.,
a picture box). I try:

mRenderWnd->Handle;

Of course, this returns an IntPtr. If I try to case, I get the error:

Cannot convert a managed type to an unmanaged type

What do I need to do? The whole point of my using managed c++ was to use
the .NET framework for GUI coding and use my existing unmanaged code for the
backend. Some of my functions require HWNDs.
 
M

Mattias Sjögren

Of course, this returns an IntPtr. If I try to case, I get the error:
Cannot convert a managed type to an unmanaged type

What do I need to do?


Try calling IntPtr::ToPointer first, then cast.


Mattias
 
Q

quat

Try calling IntPtr::ToPointer first, then cast.

Thanks, I got that error to go away, but now I get this error:

cannot convert parameter 6 from 'cli::interior_ptr<Type>' to
'IDirect3DDevice9 **'
with
[
Type=IDirect3DDevice9 *
]
Cannot convert a managed type to an unmanaged type

This is the call, which is in a method of the Form class:

d3dObject->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
(HWND)hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
d3dPP,
&d3dDevice)); <------problem

As far as I know, this is just unmanaged code calling unmanaged code inside
a managed function. Does this not work?
 
Q

quat

d3dObject->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
(HWND)hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
d3dPP,
&d3dDevice)); <------problem

Do you think I need to use pointer pinning? d3dDevice of a pointer to an
unmanaged type. But I think, if I understood some documents I read, the
address of this (i.e., &d3dDevice) will actually evaluate to a __gc pointer.
And I think that is the problem because the unmanaged function CreateDevice
expects a __nogc pointer. I also read that pinning can maybe be used to
convert a managed pointer to an unmanaged pointer.
 
T

Tamas Demjen

quat said:
Thanks, I got that error to go away, but now I get this error:

cannot convert parameter 6 from 'cli::interior_ptr<Type>' to
'IDirect3DDevice9 **'
[...]
As far as I know, this is just unmanaged code calling unmanaged code inside
a managed function. Does this not work?

Well, cli::interior_ptr is holding a pointer to a managed object. When
the garbage collector re-shuffles the memory, all interior_ptr's are
automatically updated, so they all point to the correct (possibly new)
location. In order to pass a pointer to an unmanaged function, you must
still pin it, to prevent the garbage collector from moving it around,
because unmanaged pointers can't be updated when the managed heap gets
defragmented.

If you're sure that all your types are unmanaged, you may try to move it
to an unmanaged function, and maybe use the #pragma unmanaged directive
to ensure that it's really compiled to native code. I'm not sure why the
compiler believes that your native types are somehow managed.

Tom
 

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

Similar Threads

Passing HWND for C++ wrapper 1
COM problems 5
passing unmanaged pointers 1
Use unmanaged code in VS2008 5
a question about types 1
Void pointer to managed object type 3
Problem finding DLL 2
exceptions 1

Top