By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,875 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Migrating to Managed C++

P: n/a
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);
typedef void (CbFunc2)(UnmanagedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbFunc1*,void*);
BOOL SetCallback(CbFunc2*,void*);

VOID DoSomthing(int n);

};

The native class is in a separate dll. I read Platform
Invoke article and I found an exapmle for importing C
function from dll, How can I register managed class method
as a call back function of native class?

Thanks, Shai Levi
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Shai,

I think you can exports your member function as a common CALLBACK function.
Then you can import it in the managed code. Here is a demo on how to invoke
API requiring Callback function.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
public delegate bool CallBack(int hwnd, int lParam);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}
}
please get back to me and let me know whether this does the job for you.

Best regards
Peter Huang

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
Subject: Migrating to Managed C++
Date: Tue, 15 Jul 2003 00:19:17 -0700
Lines: 28
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26165
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

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);
typedef void (CbFunc2)(UnmanagedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbFunc1*,void*);
BOOL SetCallback(CbFunc2*,void*);

VOID DoSomthing(int n);

};

The native class is in a separate dll. I read Platform
Invoke article and I found an exapmle for importing C
function from dll, How can I register managed class method
as a call back function of native class?

Thanks, Shai Levi


Nov 16 '05 #2

P: n/a
Hi Peter,
In your example (that also appears in Platform Invoke
article) the function that is imported is a c function (no
this)

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::SetCallback
(CallBack x, IntPtr y); ?
this is not working.

(I remind you that I can't change the native dll code)
-----Original Message-----
Hi Shai,

I think you can exports your member function as a common CALLBACK function.Then you can import it in the managed code. Here is a demo on how to invokeAPI requiring Callback function.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
public delegate bool CallBack(int hwnd, int lParam); public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows (CallBack x, int y); public static void Main()
{
CallBack myCallBack = new CallBack (EnumReportApp.Report); EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}
}
please get back to me and let me know whether this does the job for you.
Best regards
Peter Huang

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
Subject: Migrating to Managed C++
Date: Tue, 15 Jul 2003 00:19:17 -0700
Lines: 28
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26165NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

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);
typedef void (CbFunc2)(UnmanagedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbFunc1*,void*);
BOOL SetCallback(CbFunc2*,void*);

VOID DoSomthing(int n);

};

The native class is in a separate dll. I read Platform
Invoke article and I found an exapmle for importing C
function from dll, How can I register managed class methodas a call back function of native class?

Thanks, Shai Levi


.

Nov 16 '05 #3

P: n/a
NativeClass exports the following two memebers:

int NativeClass::SetCallback(void (*)(int ,void*),void *)
int NativeClass::SetCallback(void (*)(UnmanagedClass2&
umc2, void* p),void *)

As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbFunc1*,void*);
// BOOL SetCallback(CbFunc2*,void*);
// ....

-----Original Message-----
Hi Shai,

Can you tell me what the NativeClass exports in the dll?
You may not directly access the unmanaged dll class member function in managed code, as you mentioned in the last post.(public static extern BOOL NativeClass::SetCallback(CallBack x, IntPtr y);)
You may check what your dll exports by using the tool depends to open the dll file, the tool is included in VS.NET or VS6.
please get back to me and let me know whether this does the job for you.
Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl>

<9M**************@cpmsftngxa06.phx.gbl>
Subject: RE: Migrating to Managed C++
Date: Thu, 17 Jul 2003 07:58:01 -0700
Lines: 113
Message-ID: <0c****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNMc9FpZxdJ4ecxSmWaEpWo76iNpA==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26278NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hi Peter,
In your example (that also appears in Platform Invoke
article) the function that is imported is a c function (no this)

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::SetCallback
(CallBack x, IntPtr y); ?
this is not working.

(I remind you that I can't change the native dll code)
-----Original Message-----
Hi Shai,

I think you can exports your member function as a common
CALLBACK function.
Then you can import it in the managed code. Here is a

demo on how to invoke
API requiring Callback function.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
public delegate bool CallBack(int hwnd, int

lParam);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows

(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack

(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int

lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}
}
please get back to me and let me know whether this does

the job for you.

Best regards
Peter Huang

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
Subject: Migrating to Managed C++
Date: Tue, 15 Jul 2003 00:19:17 -0700
Lines: 28
Message-ID: <02****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE

V5.50.4910.0300Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.languages.vc:26165
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

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);
typedef void (CbFunc2)(UnmanagedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbFunc1*,void*);
BOOL SetCallback(CbFunc2*,void*);

VOID DoSomthing(int n);

};

The native class is in a separate dll. I read Platform
Invoke article and I found an exapmle for importing C
function from dll, How can I register managed class

method
as a call back function of native class?

Thanks, Shai Levi
.



.

Nov 16 '05 #4

P: n/a
Hi Shai,

It seems that your class just exports two member functions. If you don not
export the whole class, you can not call the member function with the
"this" pointer. Or, you can export the functions as static ones, then the
"this" point is not needed. Here is a simple demo in the case of export the
whole class. It consists of three files.(NativeClass.cpp, WrapClass.cpp,
ConsoleApplication2.cs)

//NativeClass.cpp It is the Native class dll [NOTE: __stdcall is necessary
for the CallBack function]
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllExport NativeClass
{
public:
NativeClass(){;}
~NativeClass(){;}
int SetCallback(CbFunc1 pfunc,int x){ return (*pfunc)(x);}
};

//WrapClass.cpp
//This file is used to wrap the NativeClass,so that it can be call with the
NativeClass instanced.
//If you exports the NativeClass SetCallback function as a static one, the
WrapClass is not necessary.
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllImport NativeClass
{
public:
NativeClass();
~NativeClass();
int SetCallback(CbFunc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFunc1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallback(pf,n);
}
};

// ConsoleApplication2.cs
//This is file is used for demonstrating invoking the SetCallBack function
in the NativeClass.
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{

public delegate bool CallBack(int hwnd, int lParam);
public delegate int CallBack1(int hwnd);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
//"?ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" can
be gotten via the depends tools.
[DllImport("c:\\WrapClass.dll",
EntryPoint="?ProxyTest@WrapClass@@SAHP6GHH@ZH@Z")]
public static extern int test(CallBack1 x,int y);
public static int tt(int x){return x*x*x;}
public static void Main()
{
CallBack1 mycb = new CallBack1(EnumReportApp.tt);
Console.WriteLine(test(mycb,6));
}
public static bool Report(int hwnd, int lParam)
{
return true;
}
}
}

I look forward to hearing from you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl> <9M**************@cpmsftngxa06.phx.gbl>
<0c****************************@phx.gbl>
<IH**************@cpmsftngxa06.phx.gbl>Subject: RE: Migrating to Managed C++
Date: Mon, 21 Jul 2003 01:02:51 -0700
Lines: 182
Message-ID: <05****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNPXnt6k37Jpf0aRnmU1iLzSyocwQ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26370
NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

NativeClass exports the following two memebers:
int NativeClass::SetCallback(void (*)(int ,void*),void *)
int NativeClass::SetCallback(void (*)(UnmanagedClass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbFunc1*,void*);
// BOOL SetCallback(CbFunc2*,void*);
// ....
-----Original Message-----
Hi Shai,

Can you tell me what the NativeClass exports in the dll?
You may not directly access the unmanaged dll class

member function in
managed code, as you mentioned in the last post.(public

static extern BOOL
NativeClass::SetCallback(CallBack x, IntPtr y);)
You may check what your dll exports by using the tool

depends to open the
dll file, the tool is included in VS.NET or VS6.
please get back to me and let me know whether this does

the job for you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and

confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl>

<9M**************@cpmsftngxa06.phx.gbl>
Subject: RE: Migrating to Managed C++
Date: Thu, 17 Jul 2003 07:58:01 -0700
Lines: 113
Message-ID: <0c****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNMc9FpZxdJ4ecxSmWaEpWo76iNpA==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gblmicrosoft.public.dotnet.languages.vc:26278NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hi Peter,
In your example (that also appears in Platform Invoke
article) the function that is imported is a c function(nothis)

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::SetCallback
(CallBack x, IntPtr y); ?
this is not working.

(I remind you that I can't change the native dll code)

-----Original Message-----
Hi Shai,

I think you can exports your member function as acommonCALLBACK function.
Then you can import it in the managed code. Here is a
demo on how to invoke
API requiring Callback function.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
public delegate bool CallBack(int hwnd, int
lParam);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows
(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack
(EnumReportApp.Report);
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int
lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}
}
please get back to me and let me know whether this does
the job for you.

Best regards
Peter Huang

--------------------
>Content-Class: urn:content-classes:message
>From: "Shai Levi" <Sh*******@verintsystems.com>
>Sender: "Shai Levi" <Sh*******@verintsystems.com>
>Subject: Migrating to Managed C++
>Date: Tue, 15 Jul 2003 00:19:17 -0700
>Lines: 28
>Message-ID: <02****************************@phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLEV5.50.4910.0300>Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
>Newsgroups: microsoft.public.dotnet.languages.vc
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.vc:26165
>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>X-Tomcat-NG: microsoft.public.dotnet.languages.vc
>
>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);
> typedef void (CbFunc2)(UnmanagedClass2& umc2,
>void* p);
> NativeClass();
> ~NativeClass();
>
> BOOL SetCallback(CbFunc1*,void*);
> BOOL SetCallback(CbFunc2*,void*);
>
> VOID DoSomthing(int n);
>
>};
>
>The native class is in a separate dll. I read Platform
>Invoke article and I found an exapmle for importing C
>function from dll, How can I register managed class
method
>as a call back function of native class?
>
>Thanks, Shai Levi
>

.


.


Nov 16 '05 #5

P: n/a
It was obvious to me that I can wrap the class
as a C API but I am trying to avoid it, I see that I can
import a class using DllImport atrinute with
CallingConvention=CallingConvention.ThisCall to import
class methods, can I use it instead of wrapping the class
with new Dll.

-----Original Message-----
Hi Shai,

It seems that your class just exports two member functions. If you don not export the whole class, you can not call the member function with the "this" pointer. Or, you can export the functions as static ones, then the "this" point is not needed. Here is a simple demo in the case of export the whole class. It consists of three files.(NativeClass.cpp, WrapClass.cpp, ConsoleApplication2.cs)

//NativeClass.cpp It is the Native class dll [NOTE: __stdcall is necessary for the CallBack function]
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllExport NativeClass
{
public:
NativeClass(){;}
~NativeClass(){;}
int SetCallback(CbFunc1 pfunc,int x){ return (*pfunc) (x);} };

//WrapClass.cpp
//This file is used to wrap the NativeClass,so that it can be call with the NativeClass instanced.
//If you exports the NativeClass SetCallback function as a static one, the WrapClass is not necessary.
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllImport NativeClass
{
public:
NativeClass();
~NativeClass();
int SetCallback(CbFunc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFunc1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallback(pf,n);
}
};

// ConsoleApplication2.cs
//This is file is used for demonstrating invoking the SetCallBack function in the NativeClass.
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{

public delegate bool CallBack(int hwnd, int lParam); public delegate int CallBack1(int hwnd);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows (CallBack x, int y); //"? ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" can be gotten via the depends tools.
[DllImport("c:\\WrapClass.dll",
EntryPoint="?ProxyTest@WrapClass@@SAHP6GHH@ZH@Z ")]
public static extern int test(CallBack1 x,int y); public static int tt(int x){return x*x*x;}
public static void Main()
{
CallBack1 mycb = new CallBack1 (EnumReportApp.tt); Console.WriteLine(test(mycb,6));
}
public static bool Report(int hwnd, int lParam) {
return true;
}
}
}

I look forward to hearing from you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl>

<9M**************@cpmsftngxa06.phx.gbl>
<0c****************************@phx.gbl>
<IH**************@cpmsftngxa06.phx.gbl>
Subject: RE: Migrating to Managed C++
Date: Mon, 21 Jul 2003 01:02:51 -0700
Lines: 182
Message-ID: <05****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNPXnt6k37Jpf0aRnmU1iLzSyocwQ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26370NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

NativeClass exports the following two memebers:
int NativeClass::SetCallback(void (*)(int ,void*),void *)
int NativeClass::SetCallback(void (*)(UnmanagedClass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbFunc1*,void*);
// BOOL SetCallback(CbFunc2*,void*);
// ....
-----Original Message-----
Hi Shai,

Can you tell me what the NativeClass exports in the dll? You may not directly access the unmanaged dll class

member function in
managed code, as you mentioned in the last post.(public

static extern BOOL
NativeClass::SetCallback(CallBack x, IntPtr y);)
You may check what your dll exports by using the tool

depends to open the
dll file, the tool is included in VS.NET or VS6.
please get back to me and let me know whether this does

the job for you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and
confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl>
<9M**************@cpmsftngxa06.phx.gbl>
Subject: RE: Migrating to Managed C++
Date: Thu, 17 Jul 2003 07:58:01 -0700
Lines: 113
Message-ID: <0c****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE

V5.50.4910.0300Thread-Index: AcNMc9FpZxdJ4ecxSmWaEpWo76iNpA==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl

microsoft.public.dotnet.languages.vc:26278
NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

Hi Peter,
In your example (that also appears in Platform Invoke
article) the function that is imported is a c function

(no
this)

[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
I can't figure out how I can import a class method -
public static extern BOOL NativeClass::SetCallback
(CallBack x, IntPtr y); ?
this is not working.

(I remind you that I can't change the native dll code)

>-----Original Message-----
>Hi Shai,
>
>I think you can exports your member function as a

common
CALLBACK function.
>Then you can import it in the managed code. Here is a
demo on how to invoke
>API requiring Callback function.
>using System;
>using System.Runtime.InteropServices;
>namespace ConsoleApplication2
>{
> public delegate bool CallBack(int hwnd, int
lParam);
> public class EnumReportApp
> {
> [DllImport("user32")]
> public static extern int EnumWindows
(CallBack x, int y);
> public static void Main()
> {
> CallBack myCallBack = new CallBack
(EnumReportApp.Report);
> EnumWindows(myCallBack, 0);
> }
>
> public static bool Report(int hwnd, int
lParam)
> {
> Console.Write("Window handle is ");
> Console.WriteLine(hwnd);
> return true;
> }
> }
>}
>please get back to me and let me know whether this does the job for you.
>
>Best regards
>Peter Huang
>
>--------------------
>>Content-Class: urn:content-classes:message
>>From: "Shai Levi" <Sh*******@verintsystems.com>
>>Sender: "Shai Levi" <Sh*******@verintsystems.com>
>>Subject: Migrating to Managed C++
>>Date: Tue, 15 Jul 2003 00:19:17 -0700
>>Lines: 28
>>Message-ID: <02****************************@phx.gbl>
>>MIME-Version: 1.0
>>Content-Type: text/plain;
>> charset="iso-8859-1"
>>Content-Transfer-Encoding: 7bit
>>X-Newsreader: Microsoft CDO for Windows 2000
>>X-MimeOLE: Produced By Microsoft MimeOLE

V5.50.4910.0300
>>Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
>>Newsgroups: microsoft.public.dotnet.languages.vc
>>Path: cpmsftngxa06.phx.gbl
>>Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.vc:26165
>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>>X-Tomcat-NG: microsoft.public.dotnet.languages.vc
>>
>>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);
>> typedef void (CbFunc2)(UnmanagedClass2& umc2,
>>void* p);
>> NativeClass();
>> ~NativeClass();
>>
>> BOOL SetCallback(CbFunc1*,void*);
>> BOOL SetCallback(CbFunc2*,void*);
>>
>> VOID DoSomthing(int n);
>>
>>};
>>
>>The native class is in a separate dll. I read Platform >>Invoke article and I found an exapmle for importing C >>function from dll, How can I register managed class
method
>>as a call back function of native class?
>>
>>Thanks, Shai Levi
>>
>
>.
>


.


.

Nov 16 '05 #6

P: n/a
Hi Shai,

To call the non-static class member function using the
¡°callingconvention.thiscall¡± in the managed class, a "this" point is
necessary. So you may need to modify your NativeClass.dll by adding two
static functions to create and destroy the class instance. In one of your
posts, you said you couldn't change the native dll code, so I didn't
mention the method. Because if you are able to change the native dll code,
you can directly modify your SetCallBack function as a static one which is
more convenient for you.

Here is a link about ¡°callingconvention.thiscall¡±.
http://groups.google.com/groups?q=ca...l=zh-CN&lr=&ie
=UTF-8&oe=UTF-8&selm=ene1ECOrAHA.2344%40tkmsftngp05&rnum=2
I look forward to hearing from you.
Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl> <9M**************@cpmsftngxa06.phx.gbl>
<0c****************************@phx.gbl>
<IH**************@cpmsftngxa06.phx.gbl>
<05****************************@phx.gbl>
<XZ**************@cpmsftngxa06.phx.gbl>Subject: RE: Migrating to Managed C++
Date: Mon, 21 Jul 2003 08:31:15 -0700
Lines: 334
Message-ID: <06****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcNPnR/QZgwv4EdnQWKtFToxXZ0kSg==
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vc:26394
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

It was obvious to me that I can wrap the class
as a C API but I am trying to avoid it, I see that I can
import a class using DllImport atrinute with
CallingConvention=CallingConvention.ThisCall to import
class methods, can I use it instead of wrapping the class
with new Dll.
-----Original Message-----
Hi Shai,

It seems that your class just exports two member

functions. If you don not
export the whole class, you can not call the member

function with the
"this" pointer. Or, you can export the functions as

static ones, then the
"this" point is not needed. Here is a simple demo in the

case of export the
whole class. It consists of three files.(NativeClass.cpp,

WrapClass.cpp,
ConsoleApplication2.cs)

//NativeClass.cpp It is the Native class dll [NOTE:

__stdcall is necessary
for the CallBack function]
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllExport NativeClass
{
public:
NativeClass(){;}
~NativeClass(){;}
int SetCallback(CbFunc1 pfunc,int x){ return (*pfunc)

(x);}
};

//WrapClass.cpp
//This file is used to wrap the NativeClass,so that it

can be call with the
NativeClass instanced.
//If you exports the NativeClass SetCallback function as

a static one, the
WrapClass is not necessary.
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport )
typedef int (__stdcall *CbFunc1)(int n);
class DllImport NativeClass
{
public:
NativeClass();
~NativeClass();
int SetCallback(CbFunc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFunc1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallback(pf,n);
}
};

// ConsoleApplication2.cs
//This is file is used for demonstrating invoking the

SetCallBack function
in the NativeClass.
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication2
{

public delegate bool CallBack(int hwnd, int

lParam);
public delegate int CallBack1(int hwnd);
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows

(CallBack x, int y);
//"?

ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" can
be gotten via the depends tools.
[DllImport("c:\\WrapClass.dll",
EntryPoint="?ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" )]
public static extern int test(CallBack1

x,int y);
public static int tt(int x){return x*x*x;}
public static void Main()
{
CallBack1 mycb = new CallBack1

(EnumReportApp.tt);
Console.WriteLine(test(mycb,6));
}
public static bool Report(int hwnd, int

lParam)
{
return true;
}
}
}

I look forward to hearing from you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and

confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "Shai Levi" <Sh*******@verintsystems.com>
Sender: "Shai Levi" <Sh*******@verintsystems.com>
References: <02****************************@phx.gbl>

<9M**************@cpmsftngxa06.phx.gbl>
<0c****************************@phx.gbl>
<IH**************@cpmsftngxa06.phx.gbl>
Subject: RE: Migrating to Managed C++
Date: Mon, 21 Jul 2003 01:02:51 -0700
Lines: 182
Message-ID: <05****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNPXnt6k37Jpf0aRnmU1iLzSyocwQ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.vc
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gblmicrosoft.public.dotnet.languages.vc:26370NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.public.dotnet.languages.vc

NativeClass exports the following two memebers:
int NativeClass::SetCallback(void (*)(int ,void*),void *)
int NativeClass::SetCallback(void (*)(UnmanagedClass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbFunc1*,void*);
// BOOL SetCallback(CbFunc2*,void*);
// ....
-----Original Message-----
Hi Shai,

Can you tell me what the NativeClass exports in thedll?You may not directly access the unmanaged dll class
member function in
managed code, as you mentioned in the last post.(public
static extern BOOL
NativeClass::SetCallback(CallBack x, IntPtr y);)
You may check what your dll exports by using the tool
depends to open the
dll file, the tool is included in VS.NET or VS6.
please get back to me and let me know whether this does
the job for you.

Best regards
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided ¡°as is¡± with no warrantiesandconfers no rights.

--------------------
>Content-Class: urn:content-classes:message
>From: "Shai Levi" <Sh*******@verintsystems.com>
>Sender: "Shai Levi" <Sh*******@verintsystems.com>
>References: <02****************************@phx.gbl>
<9M**************@cpmsftngxa06.phx.gbl>
>Subject: RE: Migrating to Managed C++
>Date: Thu, 17 Jul 2003 07:58:01 -0700
>Lines: 113
>Message-ID: <0c****************************@phx.gbl>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLEV5.50.4910.0300>Thread-Index: AcNMc9FpZxdJ4ecxSmWaEpWo76iNpA==
>Newsgroups: microsoft.public.dotnet.languages.vc
>Path: cpmsftngxa06.phx.gbl
>Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.vc:26278
>NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
>X-Tomcat-NG: microsoft.public.dotnet.languages.vc
>
>Hi Peter,
>In your example (that also appears in Platform Invoke
>article) the function that is imported is a c function
(no
>this)
>
>[DllImport("user32")]
>public static extern int EnumWindows(CallBack x, inty);>
>I can't figure out how I can import a class method -
>public static extern BOOL NativeClass::SetCallback
>(CallBack x, IntPtr y); ?
>this is not working.
>
>(I remind you that I can't change the native dll code)
>
>>-----Original Message-----
>>Hi Shai,
>>
>>I think you can exports your member function as a
common
>CALLBACK function.
>>Then you can import it in the managed code. Here is a
>demo on how to invoke
>>API requiring Callback function.
>>using System;
>>using System.Runtime.InteropServices;
>>namespace ConsoleApplication2
>>{
>> public delegate bool CallBack(int hwnd, int
>lParam);
>> public class EnumReportApp
>> {
>> [DllImport("user32")]
>> public static extern int EnumWindows
>(CallBack x, int y);
>> public static void Main()
>> {
>> CallBack myCallBack = new CallBack
>(EnumReportApp.Report);
>> EnumWindows(myCallBack, 0);
>> }
>>
>> public static bool Report(int hwnd, int
>lParam)
>> {
>> Console.Write("Window handle is ");
>> Console.WriteLine(hwnd);
>> return true;
>> }
>> }
>>}
>>please get back to me and let me know whether thisdoes>the job for you.
>>
>>Best regards
>>Peter Huang
>>
>>--------------------
>>>Content-Class: urn:content-classes:message
>>>From: "Shai Levi" <Sh*******@verintsystems.com>
>>>Sender: "Shai Levi" <Sh*******@verintsystems.com>
>>>Subject: Migrating to Managed C++
>>>Date: Tue, 15 Jul 2003 00:19:17 -0700
>>>Lines: 28
>>>Message-ID: <02****************************@phx.gbl>
>>>MIME-Version: 1.0
>>>Content-Type: text/plain;
>>> charset="iso-8859-1"
>>>Content-Transfer-Encoding: 7bit
>>>X-Newsreader: Microsoft CDO for Windows 2000
>>>X-MimeOLE: Produced By Microsoft MimeOLE
V5.50.4910.0300
>>>Thread-Index: AcNKoWcpcG037ES0R7yCWi4JcBMuqw==
>>>Newsgroups: microsoft.public.dotnet.languages.vc
>>>Path: cpmsftngxa06.phx.gbl
>>>Xref: cpmsftngxa06.phx.gbl
>microsoft.public.dotnet.languages.vc:26165
>>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>>>X-Tomcat-NG: microsoft.public.dotnet.languages.vc
>>>
>>>Hi,
>>>I'm trying to migrate native c++ class to managedc++>>>class.
>>>
>>>The native class header definition looks as:
>>>
>>>class NativeClass
>>>{
>>>public:
>>> typedef void (CbFunc1)(int n,void* p);
>>> typedef void (CbFunc2)(UnmanagedClass2& umc2,
>>>void* p);
>>> NativeClass();
>>> ~NativeClass();
>>>
>>> BOOL SetCallback(CbFunc1*,void*);
>>> BOOL SetCallback(CbFunc2*,void*);
>>>
>>> VOID DoSomthing(int n);
>>>
>>>};
>>>
>>>The native class is in a separate dll. I readPlatform>>>Invoke article and I found an exapmle for importingC>>>function from dll, How can I register managed class
>method
>>>as a call back function of native class?
>>>
>>>Thanks, Shai Levi
>>>
>>
>>.
>>
>
>
>

.


.


Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.