473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Managed C++ & P/Invoke in a web serice

I am exploring using P/Invoke to call some legacy code from our customer
contact software. I have been trying some simple test scenarios and haven't
had success. Can someone point me to an example that shows how this can be
done? Here is a snippet of code I am trying:

Header file of C++ class for web service:

[DllImport("kernel32.dll", SetLastError=true)]
extern "C" bool GetComputerName(String *name, int *len);

Class file for web service:

String __gc* callKAPIClass::HelloWorld()
{
String *p = "1234567890123456789012345678901234567890";
int len = 30;
bool rc = true;

rc = GetComputerName(p, &len);
int rc2 = Marshal::GetLastWin32Error();

return p;
}

Unfortunately, I always get an error in GetComputerName().

Mark
Nov 17 '05 #1
1 1264
MarkOwen wrote:
I am exploring using P/Invoke to call some legacy code from our
customer contact software. I have been trying some simple test
scenarios and haven't had success. Can someone point me to an example
that shows how this can be done? Here is a snippet of code I am
trying:

Header file of C++ class for web service:

[DllImport("kernel32.dll", SetLastError=true)]
extern "C" bool GetComputerName(String *name, int *len);

Class file for web service:

String __gc* callKAPIClass::HelloWorld()
{
String *p = "1234567890123456789012345678901234567890";
int len = 30;
bool rc = true;

rc = GetComputerName(p, &len);
int rc2 = Marshal::GetLastWin32Error();

return p;
}

Unfortunately, I always get an error in GetComputerName().


Not surprising, since GetComputerName doesn't have any idea what to do with
a String*.

My suggestion would be to not use PInvoke. Rather, just call the function
from your C++ code and rely on IJW to bridge the managed/unmanaged gap.

<untested code>
#include <windows.h>

String __gc* callKAPIClass::HelloWorld()
{
char sz[MAX_COMPUTERNAME_LENGTH + 1];
int len = sizeof(sz);
bool rc = true;

rc = GetComputerName(sz, &len);
int rc2 = Marshal::GetLastWin32Error();

return Marshal.PtrToStringAuto(static_cast<IntPtr>(sz),le n);
}
</untested code>

-cd


Nov 17 '05 #2

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

Similar topics

3
by: New World Order Pigs | last post by:
Sorry if this is the wrong group, but it seems like a good place to start. I have a control, creating a dll written in c#/.net and some unmanaged code in c++ using embedded c++ V3. Yes, it is for...
23
by: Bredal Jensen | last post by:
I want to port my MFC (VC++6) application to manageg VC ++. 7. I want to do this because some things are much better done with C#. So i could write a C# class and use in my VC++ code as Languague...
6
by: Shai Levi | last post by:
Hi, I'm trying to migrate native c++ class to managed c++ class. The native class header definition looks as: class NativeClass { public: typedef void (CbFunc1)(int n,void* p);
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
3
by: Tommy Svensson \(InfoGrafix\) | last post by:
I've been instructed to work againt a huge unmanaged C++ API from a C# application. Now, the only way, as I've understood it, is to go the Managed Extensions for C++ way. This means I have to...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
14
by: Notre Poubelle | last post by:
Hello, I have a worker thread that needs to call a method that only works on the main/UI thread. One way to accomplish this in managed code is to use Control.Invoke. However, in my case I...
25
by: Koliber (js) | last post by:
sorry for my not perfect english i am really f&*ckin angry in this common pattern about dispose: ////////////////////////////////////////////////////////// Public class...
0
by: Morgan Cheng | last post by:
I have one webservice written in MC++, which uses another native DLL deployed in %webservice_install_dir%\bin folder. The API invocation between MC++ and native C++ looks seamless. The compilation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.