Click here to Skip to main content
15,896,154 members

Marshalling callbacks from C++ to C#

Member 10020245 asked:

Open original thread
I have a SDK from a third party -- A camera vendor (Hasselblad) that has provided an SDK for interfacing a PC to his camera. The SDK consists of a C++ DLL and an unmanaged C++ sample application that uses the DLL. I am able to get all the methods properly marshalled into my C# code except the CALLBACK. Also, the sample application runs just fine from their unmanaged C++ source code. The DLL header for the C++ CALLBACK looks like this:

#define PHOCUS_API   __declspec( dllexport )
#define PHOCUS_API_CALL __stdcall
	typedef HRESULT (CALLBACK *STATUS_MESSAGE_CALLBACK )( void* ref, HRESULT hr, LPCWSTR pszMessage );
	PHOCUS_API HRESULT PHOCUS_API_CALL SetStatusMessageCallback( HANDLE hClient, STATUS_MESSAGE_CALLBACK pfnCallback, void* ref );


What I have tried:

I have the following import statements at the top of a simple Main entry point as well as the delegate and event definitions.

[DllImport("PhocusApi64.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern Int32 SetStatusMessageCallback(IntPtr hClient, STATUS_MESSAGE_CALLBACK PhocusMessageCallback, IntPtr refx);

public delegate Int32 STATUS_MESSAGE_CALLBACK(IntPtr refs, Int32 HRESULT, String szMessage);
public static event STATUS_MESSAGE_CALLBACK MessageResponse;


I use other DLL methods to create handles to the installed camera devices (PhocusDevices[0]). I can use these handles to do functions such as setting the aperture, getting the serial number etc. Also, I can command the camera to take a picture. The variable "Result" is returned from the DLL methods and indicate that the methods are working properly. In the sample application that I am replicating in the managed code, the CALLBACK fires properly and I can step into the C++ code. My C# code compiles w/o error and I can step through all the C# statements without exceptions. In the C# case, I cannot get the delegate to fire after commanding a image capture -- although I hear the camera trigger and can access the image. I have the following statements in my C# code.

    IntPtr Result = SetStatusMessageCallback(PhocusDevices[0], ResponseAfterImageDownload, (IntPtr)0);

static Int32 ResponseAfterImageDownload(IntPtr refs, Int32 HRESULT, String
    MessageResponse += ResponseAfterImageDownload;

static Int32 ResponseAfterImageDownload(IntPtr refs, Int32 HRESULT, String szMessage)
{
    int a = 0;
    // this method will be entered when we get a message from Phocus
    return 0;
}


Your thoughts would be appreciated ...
Jim
Tags: Marshalling

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900