quarta-feira, 15 de outubro de 2008

Re: C++ Dll Import Callback Function Question: msg#00027

Re: C++ Dll Import Callback Function Question: msg#00027

Re: C++ Dll Import Callback Function Question: msg#00027

Subject: Re: C++ Dll Import Callback Function Question
Ok, in an effort to discover WTF...  I added a couple of void Callback
functions to my c++ dll and I can get one of the void call backs to actually
work in the c# application. However the wrong void callback is being
called. What gives? Are the function pointers getting touched/changed by
the GC or something? I store the function pointers in global variables in
the c++ dll to be called by a thread later. Here is what I am now doing:


public delegate void Void1CB();
public delegate void Void2CB();

[DllImport("mydll.dll")]
public static extern void fnCPPFunction(
Void1CB cbVoid1,
Void2CB cbVoid2
);

static void Void1()
{
Console.WriteLine( "Callback Void1" );
}

static void Void2()
{
Console.WriteLine( "Callback Void2" );
}

static void Main(string[] args)
{
Void1CB cbVoid1 = new Void1CB( Void1 );
Void2CB cbVoid2 = new Void2CB( Void2 );

fnCPPFunction( cbVoid1, cbVoid2 );

Console.Read();
}


Inside the c++ dll the Void1 callback throws an exception and the weird one
is that when the c++ dll calls the Void2 callback the Void1 callback is
being called in the c# application. Almost like the function pointers are
getting FUBAR'd.

Martin Henderson
Network 1 Financial
Senior Software Engineer
1-877-470-4001

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.


-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx]On Behalf Of Martin H.
Henderson
Sent: Wednesday, December 03, 2003 1:55 PM
To: ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx
Subject: Re: [ADVANCED-DOTNET] C++ Dll Import Callback Function Question


I get no exceptions, the callback is simply never called. FYI, this works
fine from a c++ app so the DLL and the callback mechanism are fine. I just
can't get it to work with c#. This interop stuff is not as easy as they
claim it to be.

// c++ dll exported prototype

void fnCPPFunction( LPFN_CALLBACK cb );



// c++ callback definition

typedef bool (*LPFN_CALLBACK)( const char* sz1, char psz2[], unsigned long*
pulsz2Length );

// c# degelgate

public delegate bool CallbackCB( [MarshalAs(UnmanagedType.LPStr)] string s1,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder s2, ref ulong uls2Length );

// c# invoke prototype

[DllImport("mydll.dll")]
public static extern void fnCPPFunction( CallbackCB cbMyCallback );

// c# callback function

public static bool MyCallback( string s1, StringBuilder s2, ref ulong
uls2Length )
{
Console.Write( "Called back." );
return true;
}

// c# main

static void Main(string[] args)
{
CallbackCB cbMyCallback = new CallbackCB( MyCallback );

fnCPPFunction( cbMyCallback );

Console.Read();
}


1. Switched from __cdel to __stdcall and recomplied the c++ dll and now my
c# program can't find the entry point for the exported function in the c++
dll.

2. MyCallBack is static, I forgot to include that in the previous email.

3. ulong in c# is not the same as a unsigned long in c++??? I can change to
this to a plain int to simply.

Martin Henderson
Network 1 Financial
Senior Software Engineer
1-877-470-4001

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.


Martin Henderson
Network 1 Financial
Senior Software Engineer
1-877-470-4001

This email and any files transmitted with it are confidential and intended
solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the system manager.


-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx]On Behalf Of Arlie Davis
Sent: Wednesday, December 03, 2003 12:10 PM
To: ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx
Subject: Re: [ADVANCED-DOTNET] C++ Dll Import Callback Function Question


1) Make sure you handle the calling convention correctly. As far as I
know, .Net only supports using the __stdcall calling convention. So
make sure that LPFN_CALLBACK is declared using __stdcall. Otherwise, if
your C++ code is compiled with a different default calling convention,
then when the unmanaged code invokes your delegate/function pointer,
you'll explode.

2) MyCallback MUST be static.

3) "unsigned long" in C++ is NOT the same as "ulong" in C#. In C++,
this is usually an unsigned 32-bit integer -- although that may vary,
depending on the compiler. In C#, ulong is always an unsigned 64-bit
integer. Since you aren't even getting called yet, this is not likely
to be an issue -- yet. But it will once you get there.

What do you mean by "the callback fails"? Unhandled SEH exception?
Managed exception? Frogs fall from the ceiling?

Have you stepped through the assembly, from the point where the
unmanaged C++ code invokes the function point? Why not?

-- arlie



-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx] On Behalf Of Martin H.
Henderson
Sent: Wednesday, December 03, 2003 12:35 PM
To: ADVANCED-DOTNET@xxxxxxxxxxxxxxxxxxx
Subject: [ADVANCED-DOTNET] C++ Dll Import Callback Function Question


I have a c++ dll that exports a function that takes a function pointer (
callback ) as a parameter. This function ( callback ) takes 2 char*
variables as parameters. How do I prototype the c# delegate to
successfully have the delegate function called from within the c++ dll?
I am getting into the c++ dll function fine but the callback fails.

// c++ dll exported function

fnCPPFunction( LPFN_CALLBACK cb );

// c++ callback definition

typedef bool (*LPFN_CALLBACK)( const char* sz1, char psz2[], unsigned
long* pulsz2Length );

// c# delegate ????? this does not work below

public delegate bool CallbackCB(
[MarshalAs(UnmanagedType.LPStr)] string s1,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder
s2,
ref ulong uls2Length );

// never gets called

public bool MyCallback( string s1, StringBuilder s2, ref ulong
uls2Length ) {
return true;
}



Martin Henderson
Network 1 Financial
Senior Software Engineer
1-877-470-4001

This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they are
addressed. If you have received this email in error please notify the
system manager.

===================================
This list is hosted by DevelopMentorR http://www.develop.com Some .NET
courses you may be interested in:

Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

Guerrilla .NET, 8 Dec 2003, in Los Angeles
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor. http://www.develop.com
Some .NET courses you may be interested in:

Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

Guerrilla .NET, 8 Dec 2003, in Los Angeles
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor. http://www.develop.com
Some .NET courses you may be interested in:

Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

Guerrilla .NET, 8 Dec 2003, in Los Angeles
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor® http://www.develop.com
Some .NET courses you may be interested in:

Guerrilla ASP.NET, 26 Jan 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

Guerrilla .NET, 8 Dec 2003, in Los Angeles
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at http://discuss.develop.com



Nenhum comentário: