473,378 Members | 1,346 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,378 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 30056
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Shai Levi | last post by:
Hi, I'm trying to migrate native c++ class to managed c++ class. The native class header definition looks as: class NativeClass { public: typedef void (CbFunc1)(int n,void* p);
3
by: Andrew Moore | last post by:
Hi All, I have a managed C++ class that makes calls into the Win32 API. I specifically am trying to take a Handle from a .NET form and convert it to a HWND to pass to a Win32 functions. The...
3
by: dbru | last post by:
I need to pass an address of a Managed float array to a DLL. The following doesn't seem to work extern static float GetXXX( StringBuilder HWND, long nWhat, ref float lparam );
0
by: jbmeeh | last post by:
I have defined a managed C++ project to call into an unmanaged MFC project. The MFC project builds successfully. However, the build of the managed project fails because the linker cannot resolve the...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
4
by: quat | last post by:
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...
0
by: quat | last post by:
I have two unmanaged pointer in a managed form class: IDirect3D9* d3dObject; IDirect3DDevice9* d3dDevice; In a member function of the form, I call: > d3dObject->CreateDevice( >...
7
by: Paul W | last post by:
Hi everybody, This may seem like a Shell question, but I believe it belongs here. I'm trying to make a wrapper class for the SHBrowseForFolder function. This function provides for a callback...
11
by: =?ISO-8859-15?Q?Kolja_M=E4rtens?= | last post by:
Hello! I've been professionally working on java projects for several years, but have done extremely little C/C++ coding and just a few little things in VB.Net. Right now I'm trying to write a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...

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.