473,657 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling C++ from C#

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:oldsyntax.

I have this in my C# program:
public class CsStRe
{
// variables defined here.
}
CsStRe[] first = new CsStRe[numelem+1];
CsStRe[] CSCurr = CPPBack.CPPClas s.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.
Jan 18 '06 #1
5 1714
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...sdn/nospam.asp
&SD=msdn

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

Jan 19 '06 #2
Hi Richard,

Thanks for the sample project, we will make some tests on it and update you
as soon as possible.
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...sdn/nospam.asp
&SD=msdn

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

Jan 23 '06 #3
Hi Richard,

Based on the error message System.BadImage FormatException , 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:oldsyntax.

#using <CSUse.netmodul e>

void CppFun01(void)
{
printf("CppFun0 1\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:oldsyntax macpp.cpp
cl cmain.c macpp.obj /link /nodefaultlib:li bcmt

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...sdn/nospam.asp
&SD=msdn

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

Jan 26 '06 #4
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.
"Richard MSL" wrote:
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:oldsyntax.

I have this in my C# program:
public class CsStRe
{
// variables defined here.
}
CsStRe[] first = new CsStRe[numelem+1];
CsStRe[] CSCurr = CPPBack.CPPClas s.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.

Mar 7 '06 #5
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.
=============== =============== =============== =========

Mar 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2312
by: Asapi | last post by:
1. Are linkage convention and calling convention referring to the same thing? 2. Does calling convention differ between languages C and C++? 3. How does calling convention differ between static and non-static class member function? 4. Could it be possible to specify different calling convention for each "global" or each class member function? Even in a single file scope?
8
2953
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int p3); The parameters here can be read either in the forward order from p1 till p3 or reverse order from p3 till p1. Can anyone explain what is the advantage/disadvantage of either of
7
6600
by: Klaus Friese | last post by:
Hi, i'm currently working on a plugin for Adobe InDesign and i have some problems with that. I'm not really a c++ guru, maybe somebody here has an idea how to solve this. The plugin is written in C++ and it's calling a java application. This application displays a window and pushing a button is calling back the c++-plugin again.
5
3414
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
3
9076
by: Mike | last post by:
Timeout Calling Web Service I am calling a .NET 1.1 web service from an aspx page. The web service can take several minutes to complete its tasks before returning a message to the aspx page. If the web service is taking a long time to complete, the aspx page returns a ‘The operation has timed-out.’ Message to the web browser after 100 seconds. I’ve added: <httpRuntime executionTimeout="300" /> to the web.config files
2
3143
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
47
4944
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both and my c++ code should call c code.but when i tried to call a function in c code externing that function in my c++ code, i am getting unresolved external symbol error. Whatever i try its giving more and more errrors...so is it possible to merge 2...
7
2677
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
10
3249
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a programming system places on how a procedure is called and how data is passed between a calling program and procedures are called calling conventions"
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.