473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 30110
Seems like you could use a void*:

C#:
MyMethod(this.H andle.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.H andle.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*****@discus sions.microsoft .comwrote in message
news:85******** *************** ***********@mic rosoft.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(r ef 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\TestA pp\Form1.cs 67 21 TestApp

Regards,
Joachim

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:85******** *************** ***********@mic rosoft.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(r ef 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\TestA pp\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\TestA pp\Form1.cs 67 21 TestApp

Regards,
Joachim

"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:85******** *************** ***********@mic rosoft.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(r ef 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\TestA pp\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*****@discus sions.microsoft .comwrote in message
news:C5******** *************** ***********@mic rosoft.com...
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\ Test\MCPP\TestA pp\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 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,


"Willy Denoyette [MVP]" wrote:
"Joachim" <Jo*****@discus sions.microsoft .comwrote in message
news:C5******** *************** ***********@mic rosoft.com...
Thanks Willy,

However, I can't get that working:

Argument '2': cannot convert from 'ref System.IntPtr' to
'HWND__*' F:\Development\ Test\MCPP\TestA pp\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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2766
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
11045
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.
3
2903
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
1144
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...
5
3417
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...
4
1721
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
0
1179
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,
7
2232
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
11
4635
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.
0
9455
marktang
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...
0
9271
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,...
0
10031
Oralloy
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...
1
9838
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,...
0
8709
agi2029
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...
0
6534
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();...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.