C# and C++ Interlock in .NET 2.0.

G

Guest

I have an application that contains both C# and C++. I have a legitimate,
seemingly unavoidable requirement to have an interlock between the C# and the
C++. By this I mean that I have to have a method in C++ that calls a method
in C#, that in turn calls back to the original method in C++. I had this
working in .NET 1.1 by using my final exe as my source of metadata, but it
does not work in 2.0, and I want a proper solution. I have implemented a
small test case, but it is not working and I do not understand why. Here is
what I am doing.

- Make a C++ file containing a function, build it.
- Build a C# file that contains a function, that calls the C++ function. It
uses the metadata from the C++ build.
- Make a new C++ file, with the same name as the old one, that contains a
function with calls the C# function. So it uses the C# metadata.

There are two problems with this approach.

1) When I try and run it, it crashes with the following error:
System.BadImageFormatException: an attempt was made to load a program with an
incorrect format. I do not understand this error, my make file and source
files are below. The call stack showed that it was in the first call from the
C# back into the C++.
2) This is a horrible way of implementing an interlock, surely there must be
a better way of doing it, or a way of eliminating it? I require it because I
have a large volume of legacy code written in C++, and a GUI for it written
in C#, both of which I want to retain. The C++ can call the Get Input
function in C#, which can call back to application code in the C++. But the
application code can in turn call a separate Get Input process in the C#
again. It can go to a maximum depth of 5 or 6 levels at most.

I would appreciate any help that anyone can offer. Thanks.


Here is my make.bat file:

del *.obj
del *.netmodule
rem First, build the one way cpp.
copy cppfu.one cppfu.cpp
cl /LN /clr:blush:ldsyntax cppfu.cpp
pause
al cppfu.netmodule /platform:x86 /target:lib /out:cppfu.dll
rem Next build the csc.
csc /t:module /r:cppfu.dll csuse.cs
pause
rem Next build the replacement cpp that calls back.
copy cppfu.bot cppfu.cpp
cl /LN /clr:blush:ldsyntax cppfu.cpp
pause
al cppfu.netmodule /platform:x86 /target:lib /out:cppfu.dll
rem Finally, link it all together.
pause
cl /LN /clr:blush:ldsyntax macpp.cpp
cl cmain.c macpp.obj /link /nodefaultlib:libcmt

Here is the source code:

Cppfu.one

#include "stdio.h"

namespace CPPFu
{
public __gc class CPPClass{ // ref was __gc.
public: static void FuA(int i)
{
printf("In CPP A %d\n",i);
}
};
}

Csuse.cs

using System;
using CPPFu;


namespace NSinCS
{
public class CSFun
{
public static void CSFunB(int i)
{
Console.WriteLine("In FuB "+i.ToString());
if(i>0) CPPFu.CPPClass.FuA(i-1);
}
}
}

Cppfu.bot

#include "stdio.h"

#using <CSUse.netmodule>

namespace CPPFu
{
public __gc class CPPClass{ // ref was __gc.
public: static void FuA(int i)
{
printf("In CPP A %d\n",i);
if(i>0) NSinCS::CSFun::CSFunB(i-1);
}
};
}

Cmain.c

#include "stdio.h"

void mainfu(void);

void main(void)
{
printf("In CMain\n");
mainfu();
}
 
G

Gary Chang[MSFT]

Hi Richard,
1) When I try and run it, it crashes with the following error:
System.BadImageFormatException: an attempt was made
to load a program with an incorrect format.

The System.BadImageFormatException would be caused by your application
needs a qualified image(an assembly) to load the static method of
CPPFu.CPPClass.FuA. But you don't provide an corresponding assembly to your
application. The current cppfu.dll assembly is not generated from the
CPPFU.ONE which the application needs to load when the error occurs.


Thanks!

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.
======================================================
 
G

Guest

The whole point of having the cppfu.one and cppfu.bot was to substitute the
one for the other, so that the version in cppfu.bot would persist. I do not
want it to try and load cppfu.one, because the version of the function that
it contains is a useless dummy function, that is just standing in for the
real version in cppfu.bot. It seems that the system is too smart for that?
Where with 1.1 you could substitute. Is there a way to make a test case with
interlocking C++ and C# functions work?



"Gary Chang[MSFT]" said:
Hi Richard,
1) When I try and run it, it crashes with the following error:
System.BadImageFormatException: an attempt was made
to load a program with an incorrect format.

The System.BadImageFormatException would be caused by your application
needs a qualified image(an assembly) to load the static method of
CPPFu.CPPClass.FuA. But you don't provide an corresponding assembly to your
application. The current cppfu.dll assembly is not generated from the
CPPFU.ONE which the application needs to load when the error occurs.


Thanks!

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.
======================================================
 
G

Gary Chang[MSFT]

Hi Richard,
Where with 1.1 you could substitute. Is there a way to make
a test case with interlocking C++ and C# functions work?

AFAIK, the .NET framework 2.0 doesn't allow such code module substitution
as in .NET 1.1. I am afraid it is impossible to workaround such an
interlock. You need to create a new code module for the cppfu.one or
cppfu.bot to link with your application.

Thanks for your understanding!

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