471,123 Members | 833 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

C++ HWND to and from C# and Managed C++?

How can I pass a C++ HWND to and from C# and Managed C++?
Mar 6 '07 #1
15 29540
Seems like you could use a void*:

C#:
MyMethod(this.Handle.ToPointer());

C++:
public:
void MyMethod(void* handle)
{
HWND l_wnd(handle);
....
}

However, that seems to require the unsafe keyword and that the code is
compiled with the /unsafe parameter. However 2, the descriptions I have found
about how to add this compiler option (e.g.
http://forums.microsoft.com/MSDN/Sho...22815&SiteID=1) talks
about a Build subfolder under Configuration Properties. I have: General,
Debugging, C/C++, Linker, Manifest Tool, Resources, XML Document Generation,
Browse Information, Build Events, Custom Build setup, and web deployment.
Nothing called Build. Where do I find this setting?

Perhaps this is not the best / simplest way to do it. Then I would
appreciate if I could be told this.
"Joachim" wrote:
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.

"Joachim" wrote:
How can I pass a C++ HWND to and from C# and Managed C++?
Mar 6 '07 #2
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.

"Joachim" wrote:
How can I pass a C++ HWND to and from C# and Managed C++?
Mar 6 '07 #3
I found it.

"Joachim" wrote:
Seems like you could use a void*:

C#:
MyMethod(this.Handle.ToPointer());

C++:
public:
void MyMethod(void* handle)
{
HWND l_wnd(handle);
...
}

However, that seems to require the unsafe keyword and that the code is
compiled with the /unsafe parameter. However 2, the descriptions I have found
about how to add this compiler option (e.g.
http://forums.microsoft.com/MSDN/Sho...22815&SiteID=1) talks
about a Build subfolder under Configuration Properties. I have: General,
Debugging, C/C++, Linker, Manifest Tool, Resources, XML Document Generation,
Browse Information, Build Events, Custom Build setup, and web deployment.
Nothing called Build. Where do I find this setting?

Perhaps this is not the best / simplest way to do it. Then I would
appreciate if I could be told this.
"Joachim" wrote:
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.

"Joachim" wrote:
How can I pass a C++ HWND to and from C# and Managed C++?
Mar 6 '07 #4
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:85**********************************@microsof t.com...
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.

"Joachim" wrote:
>How can I pass a C++ HWND to and from C# and Managed C++?

A HANDLE is represented by an IntPtr in the framework so you simply have to pass a reference
to the IntPtr variable holding the HWND.

IntPtr hwnd = myForm.Handle;
....
// call unmanaged function expecting a pointer to a HWND.
UnmanagedFunc(ref hwnd);

Willy.

Mar 6 '07 #5
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp

Regards,
Joachim

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:85**********************************@microsof t.com...
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.

"Joachim" wrote:
How can I pass a C++ HWND to and from C# and Managed C++?


A HANDLE is represented by an IntPtr in the framework so you simply have to pass a reference
to the IntPtr variable holding the HWND.

IntPtr hwnd = myForm.Handle;
....
// call unmanaged function expecting a pointer to a HWND.
UnmanagedFunc(ref hwnd);

Willy.

Mar 6 '07 #6
That was for a HWND and not a HWND*. But for HWND the error is similar:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__**' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp
"Joachim" wrote:
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp

Regards,
Joachim

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:85**********************************@microsof t.com...
What I really want to do is to pass a C# equivalent to the HWND structure in
C++ and pass it as a pointer to a HWND in managed C++. I need to get this
window handle to a third party unmanaged C++ function to assign the third
party software to render video onto this window.
>
"Joachim" wrote:
>
>How can I pass a C++ HWND to and from C# and Managed C++?

A HANDLE is represented by an IntPtr in the framework so you simply have to pass a reference
to the IntPtr variable holding the HWND.

IntPtr hwnd = myForm.Handle;
....
// call unmanaged function expecting a pointer to a HWND.
UnmanagedFunc(ref hwnd);

Willy.
Mar 6 '07 #7
Joachim wrote:
That was for a HWND and not a HWND*. But for HWND the error is similar:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__**' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp
Can you show us the prototype you're using?

Where is this function you're calling, whose second argument you can't
find the type for?

To get that HWND__** in C#, you'd need to use an unsafe block. Where did
the function come from?

-- Barry

--
http://barrkel.blogspot.com/
Mar 6 '07 #8
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:C5**********************************@microsof t.com...
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp

Regards,
Joachim

Please show us your "DllImport" declaration, your C function prototype and how you declared
HWND__ in C#.

Willy.

Mar 6 '07 #9
// This is the main DLL file.

#include "stdafx.h"
#include "MCPP.h"

namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}
//From C#: (I included the reference dll)

IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,


"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:C5**********************************@microsof t.com...
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\Test\MCPP\TestApp\Form1.cs 67 21 TestApp

Regards,
Joachim


Please show us your "DllImport" declaration, your C function prototype and how you declared
HWND__ in C#.

Willy.

Mar 7 '07 #10
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
// This is the main DLL file.

#include "stdafx.h"
#include "MCPP.h"

namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}
//From C#: (I included the reference dll)

IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,



I'm still missing the PInvoke declaration, anyway you need something like this:

[DllImport("somedll")]
...... Test(ref IntPtr parent_wnd, ref IntPtr overlay);

but the point is, do you nee to pass a pointer to a HWND or do you need a HWND? guess not!
Note that a HWND (a HANDLE) itself is a pointer to a void. So, IMO you need to declare your
C++ function as taking two HWND, and in C# you simply need to pass an IntPtr instead of a
ref IntPtr.

Willy.

Mar 7 '07 #11
Thats odd. I can use the function without that declaration...

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
// This is the main DLL file.

#include "stdafx.h"
#include "MCPP.h"

namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}
//From C#: (I included the reference dll)

IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,

I'm still missing the PInvoke declaration, anyway you need something like this:

[DllImport("somedll")]
...... Test(ref IntPtr parent_wnd, ref IntPtr overlay);

but the point is, do you nee to pass a pointer to a HWND or do you need a HWND? guess not!
Note that a HWND (a HANDLE) itself is a pointer to a void. So, IMO you need to declare your
C++ function as taking two HWND, and in C# you simply need to pass an IntPtr instead of a
ref IntPtr.

Willy.

Mar 7 '07 #12
How would that declaration look like when I have a class?

Regards,
Joachim

"Joachim" wrote:
Thats odd. I can use the function without that declaration...

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
// This is the main DLL file.
>
#include "stdafx.h"
#include "MCPP.h"
>
namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}
>
>
//From C#: (I included the reference dll)
>
IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
>
>


I'm still missing the PInvoke declaration, anyway you need something like this:

[DllImport("somedll")]
...... Test(ref IntPtr parent_wnd, ref IntPtr overlay);

but the point is, do you nee to pass a pointer to a HWND or do you need a HWND? guess not!
Note that a HWND (a HANDLE) itself is a pointer to a void. So, IMO you need to declare your
C++ function as taking two HWND, and in C# you simply need to pass an IntPtr instead of a
ref IntPtr.

Willy.


Mar 7 '07 #13
If I remove the class declaration and only have static functions I get the
following error

"Unable to find an entry point named 'Test' in DLL 'MCPP.dll'."
[DllImport(DllName, CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Ansi)]
static extern bool Test(
ref IntPtr one, ref IntPtr two);
//The managed cpp header
#pragma once

#include "windows.h"
#include "Common.h"

namespace MCPP
{
static bool Test(
HWND parent_wnd,
HWND overlay);
}
"Joachim" wrote:
How would that declaration look like when I have a class?

Regards,
Joachim

"Joachim" wrote:
Thats odd. I can use the function without that declaration...

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
// This is the main DLL file.

#include "stdafx.h"
#include "MCPP.h"

namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}


//From C#: (I included the reference dll)

IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,


>
>
>
I'm still missing the PInvoke declaration, anyway you need something like this:
>
[DllImport("somedll")]
...... Test(ref IntPtr parent_wnd, ref IntPtr overlay);
>
but the point is, do you nee to pass a pointer to a HWND or do you need a HWND? guess not!
Note that a HWND (a HANDLE) itself is a pointer to a void. So, IMO you need to declare your
C++ function as taking two HWND, and in C# you simply need to pass an IntPtr instead of a
ref IntPtr.
>
Willy.
>
>
>
>
Mar 7 '07 #14
When I use

__declspec(dllexport)

inf front of a function which handles any kind of System::String^ I get a
compilation error. Will it do this to all managed c++ specific constructs?
What am I doing wrong? Is there another way to export functions in Managed
C++?

"Joachim" wrote:
If I remove the class declaration and only have static functions I get the
following error

"Unable to find an entry point named 'Test' in DLL 'MCPP.dll'."
[DllImport(DllName, CallingConvention = CallingConvention.Winapi,
CharSet = CharSet.Ansi)]
static extern bool Test(
ref IntPtr one, ref IntPtr two);
//The managed cpp header
#pragma once

#include "windows.h"
#include "Common.h"

namespace MCPP
{
static bool Test(
HWND parent_wnd,
HWND overlay);
}
"Joachim" wrote:
How would that declaration look like when I have a class?

Regards,
Joachim

"Joachim" wrote:
Thats odd. I can use the function without that declaration...
>
"Willy Denoyette [MVP]" wrote:
>
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:55**********************************@microsof t.com...
// This is the main DLL file.
>
#include "stdafx.h"
#include "MCPP.h"
>
namespace MCPP
{
/*static */ bool MyManagedCPPClass::Test(
HWND* parent_wnd,
HWND* overlay)
{
//Some code...
}
}
>
>
//From C#: (I included the reference dll)
>
IntPtr l_parent_hwnd = Handle;
IntPtr l_hwnd = m_panel1.Handle;
if (!MyManagedCPPClass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
>
>



I'm still missing the PInvoke declaration, anyway you need something like this:

[DllImport("somedll")]
...... Test(ref IntPtr parent_wnd, ref IntPtr overlay);

but the point is, do you nee to pass a pointer to a HWND or do you need a HWND? guess not!
Note that a HWND (a HANDLE) itself is a pointer to a void. So, IMO you need to declare your
C++ function as taking two HWND, and in C# you simply need to pass an IntPtr instead of a
ref IntPtr.

Willy.


Mar 7 '07 #15
"Joachim" <Jo*****@discussions.microsoft.comwrote in message
news:64**********************************@microsof t.com...
Thats odd. I can use the function without that declaration...
Sorry, got confused by the C++ method signature, now I see that this is managed C++, note
that you should specify which dialect of C++ you are talking about when posting, we have
MC++ (you shouldn't use any longer) and C++/CLI.
Anyway, you don't need the DllImport in your C# code, your C++/CLI method should take an
IntPtr.
When passing the IntPtr to umanaged code you need to convert the IntPtr to a point to void,
using IntPtr::ToPointer()

C#
MyManagedCPPClass c = new MyManagedCPPClass();
c.Test(IntPtr parent, IntPtr overlay-);

//C++/CLI
/*static */ bool MyManagedCPPClass::Test(
System::IntPtr parent_wnd,
System::IntPtr overlay)

....
// make it an HWND
HWND hwnd = parent_wnd.ToPointer();
// call native function expecting a HWND param..
Func(HWND hwnd..)
...

Willy.

Mar 7 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Shai Levi | last post: by
3 posts views Thread by Andrew Moore | last post: by
reply views Thread by jbmeeh | last post: by
4 posts views Thread by quat | last post: by
reply views Thread by quat | last post: by
11 posts views Thread by =?ISO-8859-15?Q?Kolja_M=E4rtens?= | last post: by

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.