470,864 Members | 1,997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,864 developers. It's quick & easy.

Getting an HWND

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.
Feb 11 '06 #1
4 1588
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 11 '06 #2
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.


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?

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 11 '06 #3
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.
Feb 11 '06 #4
quat wrote:
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
Feb 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by hellrazor | last post: by
4 posts views Thread by wallacej | last post: by
1 post views Thread by | last post: by
1 post views Thread by eagle | last post: by
2 posts views Thread by shalender verma | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.