473,543 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem calling unmanaged API from C#, works from VB.NET

Hello!
I have a .NET application that communicates with an AS400 application
through PCOM. I call an unmanaged function in the PCOM API to get a
string back representing the screen in the host system. When I call
this function from VB.NET it works fine but from C# I only get a
string containing unreadable characters back. If anyone knows what's
wrong I'd be very happy!

VB.NET code that works:
Private Declare Function hllapi Lib "PCSHLL32.D LL" (ByRef Func As
Integer, ByVal DataString As String, ByRef Length As Integer, ByRef
RetC As Integer) As Integer

Dim testStr As New String(" ", 8000)
ret = hllapi(COPY_PRE SENTATION_SPACE , testStr, HllLength,
HllReturnCode)
ResponseBuffer = testStr

testStr is the variable that contains the string I am interested in.

C# code that does not work:
[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch arSet.Auto,SetL astError=true)]
public static extern int hllapi(ref int Func,
[MarshalAs(Unman agedType.LPWStr )] string DataString, ref int Length,
ref int RetC);

string hllData = new string(' ', 8000);
int hllLength;
int hllReturnCode;
int ret;
hllLength = 0;
hllReturnCode = 0;
ret = hllapi(ref COPY_PRESENTATI ON_SPACE, hllData, ref hllLength, ref
hllReturnCode);

responseBuffer = hllData;

hllData either contains nothing or unreadable characters depending on
how I write DllImport. I have tried the following alternatives as
well:

[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch arSet.Unicode,S etLastError=tru e)]
public static extern int hllapi(ref int Func,
[MarshalAs(Unman agedType.LPWStr )] string DataString, ref int Length,
ref int RetC);
[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch arSet.Unicode,S etLastError=tru e)]
public static extern int hllapi(ref int Func, string DataString, ref
int Length, ref int RetC);

Regards
Ulrika
Nov 15 '05 #1
6 7267
Ulrika,

Can you give the original C declaration from the header file? I believe
that you should be using an Ansi string, and not a Unicode one, but without
seeing the original declaration, it is impossible to tell.

Also, I believe that the declare keyword in VB will try to default to
using the Ansi version of a function (if it exists) and marshals all string
parameters as ansi strings.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Ulrika Ziverts" <ul************ @sas.se> wrote in message
news:6b******** *************** ***@posting.goo gle.com...
Hello!
I have a .NET application that communicates with an AS400 application
through PCOM. I call an unmanaged function in the PCOM API to get a
string back representing the screen in the host system. When I call
this function from VB.NET it works fine but from C# I only get a
string containing unreadable characters back. If anyone knows what's
wrong I'd be very happy!

VB.NET code that works:
Private Declare Function hllapi Lib "PCSHLL32.D LL" (ByRef Func As
Integer, ByVal DataString As String, ByRef Length As Integer, ByRef
RetC As Integer) As Integer

Dim testStr As New String(" ", 8000)
ret = hllapi(COPY_PRE SENTATION_SPACE , testStr, HllLength,
HllReturnCode)
ResponseBuffer = testStr

testStr is the variable that contains the string I am interested in.

C# code that does not work:
[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch
arSet.Auto,SetL astError=true)] public static extern int hllapi(ref int Func,
[MarshalAs(Unman agedType.LPWStr )] string DataString, ref int Length,
ref int RetC);

string hllData = new string(' ', 8000);
int hllLength;
int hllReturnCode;
int ret;
hllLength = 0;
hllReturnCode = 0;
ret = hllapi(ref COPY_PRESENTATI ON_SPACE, hllData, ref hllLength, ref
hllReturnCode);

responseBuffer = hllData;

hllData either contains nothing or unreadable characters depending on
how I write DllImport. I have tried the following alternatives as
well:

[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch
arSet.Unicode,S etLastError=tru e)] public static extern int hllapi(ref int Func,
[MarshalAs(Unman agedType.LPWStr )] string DataString, ref int Length,
ref int RetC);
[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSet=Ch
arSet.Unicode,S etLastError=tru e)] public static extern int hllapi(ref int Func, string DataString, ref
int Length, ref int RetC);

Regards
Ulrika

Nov 15 '05 #2

The declaration in the header file looks like this:

extern long far pascal HLLAPI(WORD*, LPSTR, WORD*, WORD*);

Now I have also tried the following two alternatives and it still works
from VB but not from C#. In the C# case I now get an empty StringBuilder
back and from VB the StringBuilder contains the string I am interested
in.

VB.NET:
Private Declare Function hllapi Lib "PCSHLL32.D LL" (ByRef Func As
Integer, ByVal DataString As StringBuilder, ByRef Length As Integer,
ByRef RetC As Integer) As Integer

Dim sb As New System.Text.Str ingBuilder(8000 )
sb.Append(New String(" ", 8000))
ret = hllapi(COPY_PRE SENTATION_SPACE , sb, HllLength, HllReturnCode)

C#:
[DllImport("pcsh ll32.dll",Entry Point="hllapi", ExactSpelling=f alse,CharSe
t=CharSet.Ansi, SetLastError=tr ue)]
private static extern int hllapi(ref int Func, StringBuilder DataString,
ref int Length, ref int RetC);

StringBuilder hllData = new StringBuilder(8 000);
hllData.Append( new string(' ', 8000));
int hllLength;
int hllReturnCode;

int ret;
int connectRet;
hllLength = 0;
hllReturnCode = 0;
ret = hllapi(ref COPY_PRESENTATI ON_SPACE, hllData, ref hllLength, ref
hllReturnCode);

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Ulrika,

The only difference between the VB.NET code and the C# code once
compiled is that the VB.NET code effectively has ExactSpelling=t rue,
and you have it set to false in the C# declaration. Does it work if
you change to ExactSpelling=t rue in the C# code?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #4
The C# declaration is also using ansi. I don't know what the VB declaration
defaults to if not specified.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:#O******** ******@tk2msftn gp13.phx.gbl...
Ulrika,

The only difference between the VB.NET code and the C# code once
compiled is that the VB.NET code effectively has ExactSpelling=t rue,
and you have it set to false in the C# declaration. Does it work if
you change to ExactSpelling=t rue in the C# code?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #5
The C# declaration is also using ansi. I don't know what the VB declaration
defaults to if not specified.


It defaults to Ansi.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #6
Good to know. Thanks.

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:u5******** ******@TK2MSFTN GP09.phx.gbl...
The C# declaration is also using ansi. I don't know what the VB declarationdefaults to if not specified.


It defaults to Ansi.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #7

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

Similar topics

10
19111
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int ray, int count)" Now I'm having problems with C++ "return(false)" being True in C#. Here is the C# code. ========================= using...
8
3075
by: Nadav | last post by:
Hi, Introduction: ********************* I am writing a mixed mode application I have a COM module and a .NET module that communicate with each other. The COM exposes a custom sink interface, the .NET module implement the Sink interface ( IUnknown based ) and the COM call the methods of this interface asynchronously.
3
2280
by: James Coleman | last post by:
Hello, The following error is appearing when attempting to create a directory using the availale system.io methods: System.IO.DirectoryNotFoundException: Could not find a part of the path "D:\". at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.Directory.InternalCreateDirectory(String fullPath, String path) at...
0
1751
by: monika.saxena | last post by:
Hi all, In one of my projects which is a web based application in asp.net, a third party tool - "Frontline Solver DLL" (It is an unmanaged DLL and ..NET is calling it using the PInvoke) is used. This DLL is used for solving and optimizing some non linear problems. Problem : The problem I am facing is as the solver runs and does the
8
2340
by: Shawn B. | last post by:
Greetings, Me again. I have (roughly) the following code: HANDLE hConsoleOutput; HANDLE hConsoleInput;
3
2759
by: Mali Guven | last post by:
Hello, I have a native DLL (written in C) which is supposed to call a managed DLL (was written in C#, and an entry point was injected modifying the ildasm'd code). The exectuable calls the native DLL but the native DLL fails to load the managed DLL. The paper that addresses the 'mixed DLL problem' below does not offer any understandable...
1
3420
by: MC-Advantica | last post by:
Does anyone have a simple "Hello World" like application that demonstrates unmanaged C++ calling managed C++ developed in VS2005? I'm confused by many posts as they discuss managed extensions from VS2003, and related techniques. I have found managed to unmanaged technique very easy in VS2005, but have not been able to build anything with...
2
7240
by: =?Utf-8?B?cGh5cw==?= | last post by:
I need to write a C# application that uses unmanaged C++ code. I created a C++/CLI wrapper to C++ code and encountered the following problem. Any time I try to instantiate a wrapper in C# application the program crashes with an error "An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll, Additional...
3
8740
by: aaron.m.johnson | last post by:
Help me understand this, please. I have an older VC++ COM DLL that I'm using in a C# project. One of the COM objects takes a callback object as a parameter so that it can spin off a thread and do some long running network stuff without blocking. When I run the application the callback works as expected, but I get an error when I try to...
3
4688
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting...
0
7593
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. ...
1
7350
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...
0
7691
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5887
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4896
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...
0
3392
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...
1
1819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
973
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
639
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...

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.