RE: Callback to unmanaged code




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.





.



Relevant Pages

  • Re: protecting program memory
    ... The mprotect() function is ... int main ... Casts of a function pointer to other types or objects are illegal in ... The mprotectgoes into prior to the printf(). ...
    (comp.os.msdos.djgpp)
  • Re: function pointer tutorial may be helpful..
    ... A function pointer is a value that is the address of a function. ... static int my_function ... void young; ...
    (comp.lang.c)
  • local functions
    ... void comp(int a, int b) ... - every function that receives 'function pointer' will need to have ...
    (comp.programming.threads)
  • Re: Mapping parsed function info to a c call
    ... function through the correct function pointer. ... int f1; ... typedef union {double dval; ... ... wrappers the go in the table and are all called though the same ...
    (comp.lang.c)
  • Re: Error message getting addresses.
    ... Firstly, Win32 is off-topic, but you're question is still ... HMODULE and GetProcAddress should receive a HMODULE. ... DWORD; ... There's no need for the prefixes on the function pointer ...
    (comp.lang.cpp)