473,498 Members | 1,972 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)(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
6 2736
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3119
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...
1
3460
by: Johnson Fernandez | last post by:
Hi, Is there a tool avaialable to convert C++ and VB COM+ components to C#? Thanks, Johnson
0
1191
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...
1
1863
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...
1
1507
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...
11
5064
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,...
6
16148
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...
9
2485
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...
0
7125
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,...
0
7167
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,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
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...
0
4593
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...
0
3095
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
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 ...
1
657
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.