RE: Callback to unmanaged code
- From: Paul S <pas@xxxxxxxxxxxxxxxx>
- Date: Thu, 6 Sep 2007 05:02:02 -0700
I've read the article and I'll have to read it again.
The problem with the article and all other articles I've read so far is that
they deal with win32 dll that are loaded after the call. I have the call to
perform the callback to in the code making the origianl code
I hvae made a test sample - a cpp console app.
#include "stdafx.h"
#include "..\Win32Api\Win32Api.h"
typedef int(__stdcall * PFN_Write)(LPCTSTR);
int WriteOnConsole(LPCTSTR pszText)
{
return wprintf_s(pszText);
}
int Unmanaged_Run()
{
int i = 0;
for (int i = 0; i < 10; i++)
{
TCHAR szBuf[100];
swprintf_s(szBuf, L"Text from unmanaged: %d\n", i);
WriteOnConsole(szBuf);
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
WriteOnConsole(L"Calling un-managed ");
Unmanaged_Run()
WriteOnConsole(L"Calling managed ");
Managed_Run((PFN_Write)WriteOnConsole);
return 0;
}
I want the code executed in the Unmanaged_Run function to be executed in
managed code and the console app cannot be compiled with the /clr swith.
Just like in the article I have also implemented a Win32 library in c++
compiled with the /clr switch to put between the managed and unmanaged world.
The function skeleton looks like this:
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace ManagedEnviroment;
// what should this look like
int WINAPI Managed_Run(PFN_Write callback)
{
TestEngine engine;
try
{
engine = new TestEngine();
}
catch (Exception * ex)
{
String * msg = ex->Message;
Console::WriteLine(msg);
return 0;
}
try
{
// how do I make this
return engine->Run((__gc * )(PFN_Write)callback);
}
catch (Exception * ex)
{
String * msg = ex->get_Message();
Console::WriteLine(msg);
return -1;
}
return 0;
};
Hope this clarifys a bit more
Regards
Paul S
--
Paul S
""Walter Wang [MSFT]"" wrote:
Hi Paul,.
Thank you for your waiting.
So the key issue here is how to call a function pointer returned from
unmanaged code in C#, right?
I've spent much time on this but unfortunately it turns out it's not
possible in C# to cast the function pointer into a delegate and call it
from managed code.
A workaround is to pass the function pointer (of IntPtr data type in C#) to
the Win32 API wrapper DLL and let it call the function instead.
I've also found a related example on this issue and with the above
workaround:
#Enumerate and Host Control Panel Applets using C#. - The Code Project - C#
Programming
http://www.codeproject.com/csharp/appletengine.asp
Hope this helps. Please feel free to let me know if there's anything else I
can help.
Regards,
Walter Wang (wawang@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
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.
- Follow-Ups:
- RE: Callback to unmanaged code
- From: "Walter Wang [MSFT]"
- RE: Callback to unmanaged code
- References:
- RE: Callback to unmanaged code
- From: "Walter Wang [MSFT]"
- RE: Callback to unmanaged code
- From: "Walter Wang [MSFT]"
- RE: Callback to unmanaged code
- From: "Walter Wang [MSFT]"
- RE: Callback to unmanaged code
- Prev by Date: Re: Callback to unmanaged code
- Next by Date: Passing objects from .NET wrapper to VB6 COM
- Previous by thread: Re: Callback to unmanaged code
- Next by thread: RE: Callback to unmanaged code
- Index(es):
Relevant Pages
|