RE: Callback not being called
- From: Bruce Parker <bparker@xxxxxxxxxxxxx>
- Date: Fri, 28 Apr 2006 17:29:01 -0700
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.
- References:
- Callback not being called
- From: Bruce Parker
- RE: Callback not being called
- From: "Peter Huang" [MSFT]
- RE: Callback not being called
- From: Bruce Parker
- RE: Callback not being called
- From: "Peter Huang" [MSFT]
- Callback not being called
- Prev by Date: Re: Deploying Office 2003 Add-ins with W2K and VS.Net 2003
- Next by Date: RE: "Object does not match target type." exception with Word 2003
- Previous by thread: RE: Callback not being called
- Next by thread: Get the CCW class factory return an existing instance
- Index(es):
Relevant Pages
|