RE: Callback not being called

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hi Peter,

Thanks for the help you are providing me.

Were you able to get this code to work in an application? My unmanaged C++
code is targeted as a Smart Device DLL. My application is a smart device
application. I took the modifications you suggested and placed into mine.
The callback in the unmanaged code appears to fire, but the managed code
never receives it. I appreciate any help you can provide.



""Peter Huang" [MSFT]" wrote:

Hi,

Based on my research, you need to change two place.
1. change the CallingConvention of the C++ export function.

typedef void (__stdcall *callBackTypeReturningStrings)(LPTSTR*
ppStringArray, int cArray);

callBackTypeReturningStrings gTheCallBackReturningStrings;
unsigned int timerHandle;

void CALLBACK TimerProc(
HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime )
{
LPTSTR ppStringArray[2];

ppStringArray[0]=SysAllocString(L"string 1");
ppStringArray[1]=SysAllocString(L"string 2");

gTheCallBackReturningStrings(ppStringArray,2);
SysFreeString(ppStringArray[0]);
SysFreeString(ppStringArray[1]);
KillTimer(NULL,timerHandle);
}

extern "C" CPPDLLFUNCTIONPOINTER_API void
InitCallBackReturningStringFromTimer(callBackTypeReturningStrings
theCallBack,LPTSTR* ppStringArray, int cArray)
{
gTheCallBackReturningStrings = theCallBack;
timerHandle = SetTimer(NULL,NULL,5000,TimerProc);
}

2. Change the .NET call back function signature, we need to tell the .NET
runtime, how many items we need to marshalling. And we need to use
PtrToStringBSTR, because the SysAllocString will return a BSTR.

[DllImport(DLLPATH, CallingConvention = CallingConvention.StdCall)]
private static extern void
InitCallBackReturningStringFromTimer(CallBackDelegateReturningStringFromTime
r theCallBack, IntPtr[] ppStringArray, int nCount);
delegate void CallBackDelegateReturningStringFromTimer([In,Out,
MarshalAs(UnmanagedType.LPArray,SizeParamIndex=1)] IntPtr[] theStrings, int
nCount);
public void CallBackMethodStringFromTimer([In,Out,
MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] IntPtr[]
ppStringArray, int nCount)
{
String myString;
myString = Marshal.PtrToStringBSTR(ppStringArray[0]);
Console.WriteLine(myString);
myString = Marshal.PtrToStringBSTR(ppStringArray[1]);
Console.WriteLine(myString);
Console.WriteLine("In Callback from timer");
}

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


.



Relevant Pages

  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • RE: Callback not being called
    ... ppStringArray, int cArray); ... void CALLBACK TimerProc( ... theCallBack,LPTSTR* ppStringArray, int cArray) ... Console.WriteLine("In Callback from timer"); ...
    (microsoft.public.dotnet.framework.interop)
  • Re: communicating through callback
    ... > and it sends the data I want to the callback function. ... context pointer that is then passed into the callback function ... typedef int (handle h, void * context); ...
    (comp.lang.c)
  • Callback not being called
    ... delegate void CallBackDelegateReturningStringFromTimer(IntPtr ... MessageBox.Show("In Callback from timer"); ... typedef void (LPTSTR* ppStringArray, int ...
    (microsoft.public.dotnet.framework.interop)
  • Re: How do I create a function in my library for passing user callback function
    ... I am writing a library which will write data to a user defined callback ... void SpecifyCallbackfunction ... typedef int (const char *); ...
    (comp.lang.c)