example of unmanaged C++ calling C# ? 
Author Message
 example of unmanaged C++ calling C# ?

Does anyone have example code of unmanaged C++ calling C# ?

Thanks,
Lynn



Sat, 22 Jan 2005 05:37:18 GMT  
 example of unmanaged C++ calling C# ?
Lynn,

Is your unmanaged code using __stdcall or __cdecl calling
convention? There are several examples of interfacing with
unmanaged code that uses __stdcall calling convention.

If on the other hand you need to interface with __cdecl
code, there are no examples that I was able to find. I did
manage to create an interface layer that allowed C# to
interface with some existing C code (the C code was
looking for a callback routine that we wanted to implement
in C#; the differing calling conventions gave us fits
until we came up with a workaround). Id be happy yo give
an example of this (if this is what you need).

-tomas

Quote:
>-----Original Message-----
>Does anyone have example code of unmanaged C++ calling
C# ?

>Thanks,
>Lynn

>.



Sat, 22 Jan 2005 05:52:34 GMT  
 example of unmanaged C++ calling C# ?
---- Foo.cs (csc /t:library Foo.cs) -----
public class CSharpCode {
    public static void DoSomething(string message) {
        System.Console.WriteLine(message);
    }

Quote:
}

---- Bar.cpp (cl /clr Bar.cpp) -----
#using <mscorlib.dll>
#using <Foo.dll>

void CallCSharpCode(char *message) {
    CSharpClass::DoSomething(new System::String(message));

Quote:
}

#pragma unmanaged
int main(int argc, char **argv) {
    CallCSharpCode(argv[0]);
    return 0;

Quote:
}

The trick is to have a function whose type is unmanaged (callable from other
unmanaged code) but its code is managed (has access to CLR types).

-Sean


Quote:
> Does anyone have example code of unmanaged C++ calling C# ?

> Thanks,
> Lynn



Sat, 22 Jan 2005 06:11:21 GMT  
 example of unmanaged C++ calling C# ?
Hi Tomas,

Quote:
> Is your unmanaged code using __stdcall or __cdecl calling
> convention? There are several examples of interfacing with
> unmanaged code that uses __stdcall calling convention.

My app is a standard Win32 MFC app.  I suspect __cdecl ???

I would like to add some data manipulation and dialog via C#
to my app.

Thanks,
Lynn McGuire



Sat, 22 Jan 2005 07:15:41 GMT  
 example of unmanaged C++ calling C# ?
Hi Sean,

Great example code !  I will try it out.

Can the C# DLL call the managed C++ code ?  In other words,
can the C# DLL pass some data back up to the main exe without
having to have a very complicated call into the DLL ?

Thanks,
Lynn



Sat, 22 Jan 2005 07:50:59 GMT  
 example of unmanaged C++ calling C# ?
Standard MFC is stdcall by default, not cdecl.
The example Sean gave should work for you directly.
Good luck!

-tomas

Quote:
>-----Original Message-----
>My app is a standard Win32 MFC app.  I suspect __cdecl ???

>I would like to add some data manipulation and dialog via
C#
>to my app.



Sat, 22 Jan 2005 07:58:10 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

2. Call C# dll from unmanaged C++

3. Unmanaged C++ calling GUI in C#

4. Unmanaged C++ calling managed C# dll

5. Interoperability UnManaged C++, Managed C++, C#

6. How to call managed C++ DLL from unmanaged C++ EXE

7. Unmanaged DLL calling a C# class

8. Calling unmanaged code from ASP.NET (C#)

9. Calling unmanaged member functions from C# (again)

10. C# classes and unmanaged c++

11. __hook and __unhook of C# event in DLL from unmanaged C++

12. Unmanaged C++ dll's in C#

 

 
Powered by phpBB® Forum Software