473,841 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Migrating to Managed C++

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)(Unman agedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbF unc1*,void*);
BOOL SetCallback(CbF unc2*,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
6 2771
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 ConsoleApplicat ion2
{
public delegate bool CallBack(int hwnd, int lParam);
public class EnumReportApp
{
[DllImport("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack(EnumRe portApp.Report) ;
EnumWindows(myC allBack, 0);
}

public static bool Report(int hwnd, int lParam)
{
Console.Write(" Window handle is ");
Console.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.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: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26165
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbF unc1*,void*);
BOOL SetCallback(CbF unc2*,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
Hi Peter,
In your example (that also appears in Platform Invoke
article) the function that is imported is a c function (no
this)

[DllImport("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::Se tCallback
(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 ConsoleApplicat ion2
{
public delegate bool CallBack(int hwnd, int lParam); public class EnumReportApp
{
[DllImport("user 32")]
public static extern int EnumWindows (CallBack x, int y); public static void Main()
{
CallBack myCallBack = new CallBack (EnumReportApp. Report); EnumWindows(myC allBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write(" Window handle is ");
Console.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.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: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26165NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbF unc1*,void*);
BOOL SetCallback(CbF unc2*,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
NativeClass exports the following two memebers:

int NativeClass::Se tCallback(void (*)(int ,void*),void *)
int NativeClass::Se tCallback(void (*)(UnmanagedCl ass2&
umc2, void* p),void *)

As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbF unc1*,void*);
// BOOL SetCallback(CbF unc2*,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::S etCallback(Call Back 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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
References: <02************ *************** *@phx.gbl>

<9M*********** ***@cpmsftngxa0 6.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: AcNMc9FpZxdJ4ec xSmWaEpWo76iNpA ==
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26278NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::Se tCallback
(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 ConsoleApplicat ion2
{
public delegate bool CallBack(int hwnd, int

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

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

(EnumReportAp p.Report);
EnumWindows(myC allBack, 0);
}

public static bool Report(int hwnd, int

lParam)
{
Console.Write(" Window handle is ");
Console.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.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: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
Newsgroup s: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl

microsoft.pub lic.dotnet.lang uages.vc:26165
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
void* p);
NativeClass();
~NativeClass();

BOOL SetCallback(CbF unc1*,void*);
BOOL SetCallback(CbF unc2*,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
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.(NativeCl ass.cpp, WrapClass.cpp,
ConsoleApplicat ion2.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(CbF unc1 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(CbF unc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFun c1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallbac k(pf,n);
}
};

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

namespace ConsoleApplicat ion2
{

public delegate bool CallBack(int hwnd, int lParam);
public delegate int CallBack1(int hwnd);
public class EnumReportApp
{
[DllImport("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);
//"?ProxyTest@Wra pClass@@SAHP6GH H@ZH@Z" can
be gotten via the depends tools.
[DllImport("c:\\ WrapClass.dll",
EntryPoint="?Pr oxyTest@WrapCla ss@@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(EnumR eportApp.tt);
Console.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.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: AcNPXnt6k37Jpf0 aRnmU1iLzSyocwQ ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26370
NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

NativeClass exports the following two memebers:
int NativeClass::Se tCallback(void (*)(int ,void*),void *)
int NativeClass::Se tCallback(void (*)(UnmanagedCl ass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbF unc1*,void*);
// BOOL SetCallback(CbF unc2*,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(Cal lBack 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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
References : <02************ *************** *@phx.gbl>

<9M********** ****@cpmsftngxa 06.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: AcNMc9FpZxdJ4ec xSmWaEpWo76iNpA ==
Newsgroups : microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gblmicrosoft.publ ic.dotnet.langu ages.vc:26278NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

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

[DllImport("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);

I can't figure out how I can import a class method -
public static extern BOOL NativeClass::Se tCallback
(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 ConsoleApplicat ion2
{
public delegate bool CallBack(int hwnd, int
lParam);
public class EnumReportApp
{
[DllImport("user 32")]
public static extern int EnumWindows
(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack
(EnumReportA pp.Report);
EnumWindows(myC allBack, 0);
}

public static bool Report(int hwnd, int
lParam)
{
Console.Write(" Window handle is ");
Console.WriteLi ne(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*******@veri ntsystems.com>
>Sender: "Shai Levi" <Sh*******@veri ntsystems.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.030 0>Thread-Index: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
>Newsgroups : microsoft.publi c.dotnet.langua ges.vc
>Path: cpmsftngxa06.ph x.gbl
>Xref: cpmsftngxa06.ph x.gbl
microsoft.pu blic.dotnet.lan guages.vc:26165
>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
>void* p);
> NativeClass();
> ~NativeClass();
>
> BOOL SetCallback(CbF unc1*,void*);
> BOOL SetCallback(CbF unc2*,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
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
CallingConventi on=CallingConve ntion.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.(NativeCl ass.cpp, WrapClass.cpp, ConsoleApplica tion2.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(CbF unc1 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(CbF unc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFun c1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallbac k(pf,n);
}
};

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

namespace ConsoleApplicat ion2
{

public delegate bool CallBack(int hwnd, int lParam); public delegate int CallBack1(int hwnd);
public class EnumReportApp
{
[DllImport("user 32")]
public static extern int EnumWindows (CallBack x, int y); //"? ProxyTest@WrapC lass@@SAHP6GHH@ ZH@Z" can be gotten via the depends tools.
[DllImport("c:\\ WrapClass.dll",
EntryPoint="?P roxyTest@WrapCl ass@@SAHP6GHH@Z H@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.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
References: <02************ *************** *@phx.gbl>

<9M*********** ***@cpmsftngxa0 6.phx.gbl>
<0c*********** *************** **@phx.gbl>
<IH*********** ***@cpmsftngxa0 6.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: AcNPXnt6k37Jpf0 aRnmU1iLzSyocwQ ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26370NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

NativeClass exports the following two memebers:
int NativeClass::Se tCallback(void (*)(int ,void*),void *)
int NativeClass::Se tCallback(void (*)(UnmanagedCl ass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registering to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbF unc1*,void*);
// BOOL SetCallback(CbF unc2*,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(Ca llBack 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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
Reference s: <02************ *************** *@phx.gbl>
<9M********* *****@cpmsftngx a06.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: AcNMc9FpZxdJ4ec xSmWaEpWo76iNpA ==
Newsgroup s: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl

microsoft.pub lic.dotnet.lang uages.vc:26278
NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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("user 32")]
public static extern int EnumWindows(Cal lBack x, int y);
I can't figure out how I can import a class method -
public static extern BOOL NativeClass::Se tCallback
(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 ;
>namespac e ConsoleApplicat ion2
>{
> public delegate bool CallBack(int hwnd, int
lParam);
> public class EnumReportApp
> {
> [DllImport("user 32")]
> public static extern int EnumWindows
(CallBack x, int y);
> public static void Main()
> {
> CallBack myCallBack = new CallBack
(EnumReport App.Report);
> EnumWindows(myC allBack, 0);
> }
>
> public static bool Report(int hwnd, int
lParam)
> {
> Console.Write(" Window handle is ");
> Console.WriteLi ne(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*******@veri ntsystems.com>
>>Sender: "Shai Levi" <Sh*******@veri ntsystems.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.03 00
>>Thread-Index: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
>>Newsgroup s: microsoft.publi c.dotnet.langua ges.vc
>>Path: cpmsftngxa06.ph x.gbl
>>Xref: cpmsftngxa06.ph x.gbl
microsoft.p ublic.dotnet.la nguages.vc:2616 5
>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
>>void* p);
>> NativeClass();
>> ~NativeClass();
>>
>> BOOL SetCallback(CbF unc1*,void*);
>> BOOL SetCallback(CbF unc2*,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 >>functio n from dll, How can I register managed class
method
>>as a call back function of native class?
>>
>>Thanks, Shai Levi
>>
>
>.
>


.


.

Nov 16 '05 #6
Hi Shai,

To call the non-static class member function using the
¡°callingconven tion.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 ¡°callingconven tion.thiscall¡± .
http://groups.google.com/groups?q=ca...l=zh-CN&lr=&ie
=UTF-8&oe=UTF-8&selm=ene1ECOr AHA.2344%40tkms ftngp05&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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.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/QZgwv4EdnQWKtFT oxXZ0kSg==
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26394
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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
CallingConvent ion=CallingConv ention.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.(NativeCl ass.cpp,

WrapClass.cp p,
ConsoleApplic ation2.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(CbF unc1 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(CbF unc1 pfunc,int x);
};
class WrapClass
{
//NativeClass* m_pc;
public:
WrapClass(){;}//m_pc=new NativeClass();}
~WrapClass(){;}//delete m_pc;}
DllExport static int ProxyTest(CbFun c1 pf,int n)
{
NativeClass m_nc;
return m_nc.SetCallbac k(pf,n);
}
};

// ConsoleApplicat ion2.cs
//This is file is used for demonstrating invoking the

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

namespace ConsoleApplicat ion2
{

public delegate bool CallBack(int hwnd, int

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

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

ProxyTest@Wrap Class@@SAHP6GHH @ZH@Z" can
be gotten via the depends tools.
[DllImport("c:\\ WrapClass.dll",
EntryPoint="? ProxyTest@WrapC lass@@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.WriteLi ne(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*******@veri ntsystems.com>
Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
References : <02************ *************** *@phx.gbl>

<9M********** ****@cpmsftngxa 06.phx.gbl>
<0c********** *************** ***@phx.gbl>
<IH********** ****@cpmsftngxa 06.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: AcNPXnt6k37Jpf0 aRnmU1iLzSyocwQ ==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups : microsoft.publi c.dotnet.langua ges.vc
Path: cpmsftngxa06.ph x.gbl
Xref: cpmsftngxa06.ph x.gblmicrosoft.publ ic.dotnet.langu ages.vc:26370NNTP-Posting-Host: TK2MSFTNGXA09 10.40.1.161
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

NativeClas s exports the following two memebers:
int NativeClass::Se tCallback(void (*)(int ,void*),void *)
int NativeClass::Se tCallback(void (*)(UnmanagedCl ass2&
umc2, void* p),void *)
As you recall these members provide the interface for
registerin g to callbacks
// class NativeClass
// {
// ...
// BOOL SetCallback(CbF unc1*,void*);
// BOOL SetCallback(CbF unc2*,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(C allBack 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*******@veri ntsystems.com>
>Sender: "Shai Levi" <Sh*******@veri ntsystems.com>
>References : <02************ *************** *@phx.gbl>
<9M******** ******@cpmsftng xa06.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.030 0>Thread-Index: AcNMc9FpZxdJ4ec xSmWaEpWo76iNpA ==
>Newsgroups : microsoft.publi c.dotnet.langua ges.vc
>Path: cpmsftngxa06.ph x.gbl
>Xref: cpmsftngxa06.ph x.gbl
microsoft.pu blic.dotnet.lan guages.vc:26278
>NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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("user 32")]
>public static extern int EnumWindows(Cal lBack x, inty);>
>I can't figure out how I can import a class method -
>public static extern BOOL NativeClass::Se tCallback
>(CallBac k 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 ;
>>namespa ce ConsoleApplicat ion2
>>{
>> public delegate bool CallBack(int hwnd, int
>lParam);
>> public class EnumReportApp
>> {
>> [DllImport("user 32")]
>> public static extern int EnumWindows
>(CallBac k x, int y);
>> public static void Main()
>> {
>> CallBack myCallBack = new CallBack
>(EnumRepor tApp.Report);
>> EnumWindows(myC allBack, 0);
>> }
>>
>> public static bool Report(int hwnd, int
>lParam)
>> {
>> Console.Write(" Window handle is ");
>> Console.WriteLi ne(hwnd);
>> return true;
>> }
>> }
>>}
>>please get back to me and let me know whether thisdoes>the job for you.
>>
>>Best regards
>>Peter Huang
>>
>>--------------------
>>>Conten t-Class: urn:content-classes:message
>>>From: "Shai Levi" <Sh*******@veri ntsystems.com>
>>>Sender : "Shai Levi" <Sh*******@veri ntsystems.com>
>>>Subjec t: Migrating to Managed C++
>>>Date: Tue, 15 Jul 2003 00:19:17 -0700
>>>Lines: 28
>>>Messag e-ID: <02************ *************** *@phx.gbl>
>>>MIME-Version: 1.0
>>>Conten t-Type: text/plain;
>>> charset="iso-8859-1"
>>>Conten t-Transfer-Encoding: 7bit
>>>X-Newsreader: Microsoft CDO for Windows 2000
>>>X-MimeOLE: Produced By Microsoft MimeOLE
V5.50.4910.0 300
>>>Thread-Index: AcNKoWcpcG037ES 0R7yCWi4JcBMuqw ==
>>>Newsgrou ps: microsoft.publi c.dotnet.langua ges.vc
>>>Path: cpmsftngxa06.ph x.gbl
>>>Xref: cpmsftngxa06.ph x.gbl
>microsoft. public.dotnet.l anguages.vc:261 65
>>>NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
>>>X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.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)(Unman agedClass2& umc2,
>>>void* p);
>>> NativeClass();
>>> ~NativeClass();
>>>
>>> BOOL SetCallback(CbF unc1*,void*);
>>> BOOL SetCallback(CbF unc2*,void*);
>>>
>>> VOID DoSomthing(int n);
>>>
>>>};
>>>
>>>The native class is in a separate dll. I readPlatform>>>Invoke article and I found an exapmle for importingC>>>functi on 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3143
by: Shailesh | last post by:
Metalink says that, whenever we migrate from dictionary managed to locally managed tablespaces, 'Migrated tablespaces are not subject to the UNIFORM/SYSTEM policy of newly created locally managed tablespaces. you should see "USER" value in the ALLOCATION_TYPE column for migrated tablespaces'. Oracle documentation says that parameters like INITIAL, NEXT , MINEXTENTS, MAXEXTENTS will not apply to locally managed tablespaces. Suppose I...
1
3483
by: Johnson Fernandez | last post by:
Hi, Is there a tool avaialable to convert C++ and VB COM+ components to C#? Thanks, Johnson
0
1217
by: michael | last post by:
I am migrating C++ unmanaged to managed VC 7.1. I continue to have problems with try catch blocks through-out my code. The compile tells me the the code is unreachable C2407 at the catch portion of the block. I am using /clr /EHsc switches. The class does not have the prefix "public _gc class CAccountExposure : System::Windows::Forms::Form " but "class CAccountExposure : public CDialog." The following is a snippet of code representing the...
1
1874
by: Steve | last post by:
We are considering the ways to migrate our VC++ 6.0 applications to .NET platform. It would be nice to rewrite them completely in C#, but due to the time constraints this option is out of question. We started from rewriting some dll in C# in Visual Studio .NET. Now we face chalenges of interoperation between old VC++ 6.0 apps and new .NET C# components. To solve this problem we considered the following options:
1
1529
by: Tony Johansson | last post by:
Hello!! You may correct me if I say something that is not right. If I want to use the .NET framework for an VC6/MFC application I must compile the VC6/MFC to VC8 using the /CLI switch after removing all the errors? The old MFC will work in the new VS2005(VC8) My MFC project consist of several files that constitute the MFC application.
11
5098
by: danip | last post by:
Hi there, I need to do the following: 1. Migrate a whole solution (with tens of projects) from VC 6.0 to VC 8.0. The project has a lot of MFC templates like CArray, and many error are poping out, like: Error 1 error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' d:\program files\microsoft visual studio 8\vc\atlmfc\include\afxtempl.h 272. 2. Also few libraries I would like to convert to managed...
6
16195
by: phnimx | last post by:
I'm attempting to migrate a predominately MFC application that I've just inherited from Visual Studio.NET 2003 to Visual Studio 2005. I've managed to clean up a myriad of compile and link errors but I'm stuck on one final problem. Please note that this application compiled/linked and ran just fine out of Visual Studio.NET 2003. Details: It's originally incarnated as an MFC app, i.e. CWinApp, CMultiDocTemplate,
9
2508
by: WT | last post by:
Hello, I have code created with .net 1.0 and migrated to 3.5. Form 2.0 the XslTransform class is obsolete and the vs2008 compiler generates warnings that these classes are absolete suggesting to use XslCompiledTransform. But all this was rendered using an Xml control and I can't find a way to relate this control to an XslCompiledTransform ? Any help appreciated to solve this migration pb.
0
9865
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
9709
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
9446
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...
1
7853
isladogs
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...
0
7025
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
5694
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...
0
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4498
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
2
4085
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.