How can I pass a C++ HWND to and from C# and Managed C++?
Mar 6 '07
15 30112
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:55******** *************** ***********@mic rosoft.com...
// This is the main DLL file.
#include "stdafx.h"
#include "MCPP.h"
namespace MCPP
{
/*static */ bool MyManagedCPPCla ss::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 (!MyManagedCPPC lass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
I'm still missing the PInvoke declaration, anyway you need something like this:
[DllImport("some dll")]
...... 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.
Thats odd. I can use the function without that declaration...
"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:55******** *************** ***********@mic rosoft.com...
// This is the main DLL file.
#include "stdafx.h"
#include "MCPP.h"
namespace MCPP
{
/*static */ bool MyManagedCPPCla ss::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 (!MyManagedCPPC lass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
I'm still missing the PInvoke declaration, anyway you need something like this:
[DllImport("some dll")]
...... 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.
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*****@discus sions.microsoft .comwrote in message
news:55******** *************** ***********@mic rosoft.com...
// This is the main DLL file.
>
#include "stdafx.h"
#include "MCPP.h"
>
namespace MCPP
{
/*static */ bool MyManagedCPPCla ss::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 (!MyManagedCPPC lass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
>
>
I'm still missing the PInvoke declaration, anyway you need something like this:
[DllImport("some dll")]
...... 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.
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(DllNa me, CallingConventi on = CallingConventi on.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*****@discus sions.microsoft .comwrote in message
news:55******** *************** ***********@mic rosoft.com...
// This is the main DLL file.
#include "stdafx.h"
#include "MCPP.h"
namespace MCPP
{
/*static */ bool MyManagedCPPCla ss::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 (!MyManagedCPPC lass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
>
>
>
I'm still missing the PInvoke declaration, anyway you need something like this:
>
[DllImport("some dll")]
...... 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.
>
>
>
>
When I use
__declspec(dlle xport)
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(DllNa me, CallingConventi on = CallingConventi on.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*****@discus sions.microsoft .comwrote in message
news:55******** *************** ***********@mic rosoft.com...
// This is the main DLL file.
>
#include "stdafx.h"
#include "MCPP.h"
>
namespace MCPP
{
/*static */ bool MyManagedCPPCla ss::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 (!MyManagedCPPC lass.Test(
0,
ref l_parent_hwnd,
ref l_hwnd,
>
>
I'm still missing the PInvoke declaration, anyway you need something like this:
[DllImport("some dll")]
...... 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.
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:64******** *************** ***********@mic rosoft.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::ToPoint er()
C#
MyManagedCPPCla ss c = new MyManagedCPPCla ss();
c.Test(IntPtr parent, IntPtr overlay-);
//C++/CLI
/*static */ bool MyManagedCPPCla ss::Test(
System::IntPtr parent_wnd,
System::IntPtr overlay)
....
// make it an HWND
HWND hwnd = parent_wnd.ToPo inter();
// call native function expecting a HWND param..
Func(HWND hwnd..)
...
Willy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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 code works properly but I get the
following compiler warning:
warning C4312: 'reinterpret_cast' : conversion from 'int' to 'HWND' of
greater size.
|
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
);
|
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 external symbols for the methods defined in the unmanaged class. Attached is the class definition for the managed class
public __gc class MTracke
public
MTracker(Control* pControl,Control* cControl
m_CTracker = new CComTrackerApp()
IntPtr...
|
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 part attempting to do
what is this article by Nish:
http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw
I have to hook up a unmanaged callback to a managed method using IJW
NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
| |
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 get the error:
Cannot convert a managed type to an unmanaged type
|
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(
> D3DADAPTER_DEFAULT,
> D3DDEVTYPE_HAL,
|
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 to your code for certain situations. I've
done this in c# but can't seem to get it to work here.
This is the delegate:
public __delegate int BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM
|
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 Windows Service in VC++ .Net thats
supposed to use a 3rd party SDK do receive Image data and send it out
through a webservice.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |