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.
--------------------[color=blue]
>Content-Class: urn:content-classes:message
>From: "Shai Levi" <Shai.Levi@verintsystems.com>
>Sender: "Shai Levi" <Shai.Levi@verintsystems.com>
>References: <021f01c34aa1$6729ab40$a001280a@phx.gbl>[/color]
<9MeX0kETDHA.2580@cpmsftngxa06.phx.gbl>
<0c0201c34c73$d16bf890$a401280a@phx.gbl>
<IHisEATTDHA.2416@cpmsftngxa06.phx.gbl>
<053901c34f5e$7b7a5240$a101280a@phx.gbl>
<XZTbwL4TDHA.2344@cpmsftngxa06.phx.gbl>[color=blue]
>Subject: RE: Migrating to Managed C++
>Date: Mon, 21 Jul 2003 08:31:15 -0700
>Lines: 334
>Message-ID: <067901c34f9d$1fd04af0$a001280a@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.[color=green]
>>-----Original Message-----
>>Hi Shai,
>>
>>It seems that your class just exports two member[/color]
>functions. If you don not[color=green]
>>export the whole class, you can not call the member[/color]
>function with the[color=green]
>>"this" pointer. Or, you can export the functions as[/color]
>static ones, then the[color=green]
>>"this" point is not needed. Here is a simple demo in the[/color]
>case of export the[color=green]
>>whole class. It consists of three files.(NativeClass.cpp,[/color]
>WrapClass.cpp,[color=green]
>>ConsoleApplication2.cs)
>>
>>//NativeClass.cpp It is the Native class dll [NOTE:[/color]
>__stdcall is necessary[color=green]
>>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)[/color]
>(x);}[color=green]
>>};
>>
>>//WrapClass.cpp
>>//This file is used to wrap the NativeClass,so that it[/color]
>can be call with the[color=green]
>>NativeClass instanced.
>>//If you exports the NativeClass SetCallback function as[/color]
>a static one, the[color=green]
>>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[/color]
>SetCallBack function[color=green]
>>in the NativeClass.
>>using System;
>>using System.Runtime.InteropServices;
>>
>>namespace ConsoleApplication2
>>{
>>
>> public delegate bool CallBack(int hwnd, int[/color]
>lParam);[color=green]
>> public delegate int CallBack1(int hwnd);
>> public class EnumReportApp
>> {
>> [DllImport("user32")]
>> public static extern int EnumWindows[/color]
>(CallBack x, int y);[color=green]
>> //"?[/color]
>ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" can[color=green]
>>be gotten via the depends tools.
>> [DllImport("c:\\WrapClass.dll",
>>EntryPoint="?ProxyTest@WrapClass@@SAHP6GHH@ZH@Z" )]
>> public static extern int test(CallBack1[/color]
>x,int y);[color=green]
>> public static int tt(int x){return x*x*x;}
>> public static void Main()
>> {
>> CallBack1 mycb = new CallBack1[/color]
>(EnumReportApp.tt);[color=green]
>> Console.WriteLine(test(mycb,6));
>> }
>> public static bool Report(int hwnd, int[/color]
>lParam)[color=green]
>> {
>> 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[/color]
>confers no rights.[color=green]
>>
>>--------------------[color=darkred]
>>>Content-Class: urn:content-classes:message
>>>From: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>Sender: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>References: <021f01c34aa1$6729ab40$a001280a@phx.gbl>[/color]
>><9MeX0kETDHA.2580@cpmsftngxa06.phx.gbl>
>><0c0201c34c73$d16bf890$a401280a@phx.gbl>
>><IHisEATTDHA.2416@cpmsftngxa06.phx.gbl>[color=darkred]
>>>Subject: RE: Migrating to Managed C++
>>>Date: Mon, 21 Jul 2003 01:02:51 -0700
>>>Lines: 182
>>>Message-ID: <053901c34f5e$7b7a5240$a101280a@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[/color][/color]
>microsoft.public.dotnet.languages.vc:26370[color=green][color=darkred]
>>>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[/color][/color]
>dll?[color=green][color=darkred]
>>>>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[/color][/color]
>and[color=green][color=darkred]
>>>confers no rights.
>>>>
>>>>--------------------
>>>>>Content-Class: urn:content-classes:message
>>>>>From: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>>>Sender: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>>>References: <021f01c34aa1$6729ab40$a001280a@phx.gbl>
>>>><9MeX0kETDHA.2580@cpmsftngxa06.phx.gbl>
>>>>>Subject: RE: Migrating to Managed C++
>>>>>Date: Thu, 17 Jul 2003 07:58:01 -0700
>>>>>Lines: 113
>>>>>Message-ID: <0c0201c34c73$d16bf890$a401280a@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[/color][/color]
>V5.50.4910.0300[color=green][color=darkred]
>>>>>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, int[/color][/color]
>y);[color=green][color=darkred]
>>>>>
>>>>>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[/color][/color]
>does[color=green][color=darkred]
>>>>>the job for you.
>>>>>>
>>>>>>Best regards
>>>>>>Peter Huang
>>>>>>
>>>>>>--------------------
>>>>>>>Content-Class: urn:content-classes:message
>>>>>>>From: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>>>>>Sender: "Shai Levi" <Shai.Levi@verintsystems.com>
>>>>>>>Subject: Migrating to Managed C++
>>>>>>>Date: Tue, 15 Jul 2003 00:19:17 -0700
>>>>>>>Lines: 28
>>>>>>>Message-ID: <021f01c34aa1$6729ab40$a001280a@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[/color][/color]
>c++[color=green][color=darkred]
>>>>>>>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[/color][/color]
>Platform[color=green][color=darkred]
>>>>>>>Invoke article and I found an exapmle for importing[/color][/color]
>C[color=green][color=darkred]
>>>>>>>function from dll, How can I register managed class
>>>>>method
>>>>>>>as a call back function of native class?
>>>>>>>
>>>>>>>Thanks, Shai Levi
>>>>>>>
>>>>>>
>>>>>>.
>>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>.
>>>>
>>>[/color]
>>
>>.
>>[/color]
>[/color]