Calling C++ from C#

G

Guest

I am porting my application from .NET 1.1 to 2.0. It worked fine in 1.1, but
will not compile in 2.0. I have this function in a c++ file:

namespace CPPBack
{
public __gc class CPPClass{
public: static CsStRe* RunStr2(CsStRe *ifirst[],int index,String *str2)[]
{
// code is in here.
}
};
}

I compile it with /clr:blush:ldsyntax.

I have this in my C# program:


public class CsStRe
{
// variables defined here.
}
CsStRe[] first = new CsStRe[numelem+1];
CsStRe[] CSCurr = CPPBack.CPPClass.RunStr2(first, CurKtr, Str2);

The C# build fails, with error CS1502 and CS1503 on the first parameter of
RunStr2, saying that the parameter is the wrong type. Is there a way to
convert it so that it will work? Please let me know of any suggestions. Thank
you.
 
G

Gary Chang[MSFT]

Hi Richard,
The C# build fails, with error CS1502 and CS1503 on the first
parameter of RunStr2, saying that the parameter is the wrong type.

Your code snippet seems no problem, I perform some tests with it on my
side, besides some link errors while converting the managed C++ class
library project, there is no CS1502 and CS1503 errors while building
converted C# project.

Would you please provide some more detailed repro steps or a simple repro
solution(zipped) to us for research this issue, you can send it to me
directly if it is not inconvenient to you.(Please remove the "online" of my
email alias.)

Thanks for your understanding!


Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang[MSFT]

Hi Richard,

Based on the error message System.BadImageFormatException, the application
needs a qualified image to load the static method of
CPPFu.CPPClass.FirstFu(); but you don't provide an assembly corresponds to
it, so this would be the reason to get that exception.

On this point, I suggest you build an assembly to provide the function
CPPFu.CPPClass.FirstFu(), you can use the AL.exe to generate the required
assembly from its .netmodule.

Since I cannot build your csuse.cs and macpp.cpp due to the interlock
reference issue, I extract the CPPFu.CPPClass definition to a stand-alone
cpp source file, and build an assembly from it for the use of csuse.cs.
Please refer to the following sample code:

//cppfu.cpp
#include <stdio.h>

namespace CPPFu
{
public __gc class CPPClass{ // ref was __gc.
public: static void FirstFu(void)
{
printf("In FirstFu\n");
}
};
}

//MACPP.cpp
#include <stdio.h> // 20050303 for printf.
#include <malloc.h>

// 20060121 Cpp program with main to be called by CS.
// This program to be compiled with /clr:blush:ldsyntax.

#using <CSUse.netmodule>

void CppFun01(void)
{
printf("CppFun01\n");
}

extern "C"{
// in manc.cpp, called by cmain.c
// Duplicate prototypes are in cmain.c.
void mainfu(void);
}
void mainfu(void)
{
printf("In CPP Main\n");
CppFun01();
NSinCS::CSFun::CSFun01();
NSinCS::CSFun::CSFun02();
}

//MAKE.bat

cl /LN /clr: oldsyntax cppfu.cpp
al cppfu.netmodule /platform:x86 /target:lib /out:cppfu.dll
csc /t:module /r:cppfu.dll csuse.cs
cl /LN /clr:blush:ldsyntax macpp.cpp
cl cmain.c macpp.obj /link /nodefaultlib:libcmt

By the way, if you can build your csuse.cs and macpp.cpp module without the
interlock problem, maybe you don't need to split the CPPFu.CPPClass
definition from the macpp.cpp file, in this scenario, you need to build an
assembly from the macpp.netmodule and reference it when building the
csuse.netmodule:
¡­
al macpp.netmodule /platform:x86 /target:lib /out:macpp.dll
csc /t:module /r:macpp.dll csuse.cs
¡­

Wish it helps!

Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Just a follow up in case anyone has a similar problem, I was able (it
appears) to get the interlock that I wanted by using a delegate, sending the
pointer from C++ to C#, which called back the C++. Thanks again Gary for your
help.
 
G

Gary Chang[MSFT]

That's great, Richard! I am glad to know you resolved this issue.

Good Luck!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
======================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top