473,395 Members | 1,631 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,395 software developers and data experts.

Calling Win32 DLL -Data Missing

Hi Guys
I am calling a simple win32 dll from a C# application.

Say the function is GetLastErrorString(BSTR *pstr)
the C#client calls it as

[DllImport("..\\..\\..\\ClientDll\\Debug\\ClientDll .dll",EntryPoint="?GetLas
tErrorString@@YAHPAPAG@Z")]
static extern int GetLastErrorString(ref string pStrErrorCode);

CLIENTDLL_API int GetLastErrorString(BSTR *pszErrorCode)
{
return gOcr.GetLastErrorString(pszErrorCode);
}

the pszErrorCode contains "Operation Success"

C# function is
string strerror=" ";
int ns = GetLastErrorString(ref strerror);

but it contains Just "O" the others are missing???

Why?
Expecting replies
Badrinath
Nov 18 '05 #1
6 1296
Hello,

You should use the StringBuilder object for a Windows API call that accepts
a string buffer to be modified by a method.
[DllImport(@"c:\Temp\ClientDll.dll",
EntryPoint=?GetLastErrorString@@YAHPAPAG@Z)]
static extern in GetLastErrorString(StringBuilder errorCode);

Regards,
Michael Zino


"Badrinath Mohan" <bm****@NOSPAMuncc.edu> wrote in message
news:uP*************@TK2MSFTNGP11.phx.gbl...
Hi Guys
I am calling a simple win32 dll from a C# application.

Say the function is GetLastErrorString(BSTR *pstr)
the C#client calls it as

[DllImport("..\\..\\..\\ClientDll\\Debug\\ClientDll .dll",EntryPoint="?GetLas tErrorString@@YAHPAPAG@Z")]
static extern int GetLastErrorString(ref string pStrErrorCode);

CLIENTDLL_API int GetLastErrorString(BSTR *pszErrorCode)
{
return gOcr.GetLastErrorString(pszErrorCode);
}

the pszErrorCode contains "Operation Success"

C# function is
string strerror=" ";
int ns = GetLastErrorString(ref strerror);

but it contains Just "O" the others are missing???

Why?
Expecting replies
Badrinath

Nov 18 '05 #2
Badrinath,
Say the function is GetLastErrorString(BSTR *pstr)
the C#client calls it as

[DllImport("..\\..\\..\\ClientDll\\Debug\\ClientDll .dll",EntryPoint="?GetLas
tErrorString@@YAHPAPAG@Z")]
static extern int GetLastErrorString(ref string pStrErrorCode);


Try it like this instead

static extern int GetLastErrorString([MarshalAs(UnmanagedType.BStr)]
ref string pStrErrorCode);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 18 '05 #3
"Michael Zino" <mi******@bloombit.com> wrote in message news:u4**************@TK2MSFTNGP12.phx.gbl...
Hello,

You should use the StringBuilder object for a Windows API call that accepts
a string buffer to be modified by a method.


Why?

--
Michael Culley
Nov 18 '05 #4
Dear Michael,

String are immutable objects in managed code, and it's OK to use it as
buffers that will not be changed by the native API function.
In this particular case (static extern int GetLastErrorString(ref string
errorCode)), the API is supposed to modify the input string
but the passed string was already created and therefore cannot be changed by
the native API function.
That's the reason why using StringBuilder instead.
You can find more information about P/Invoke and C# in the following link:
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Regards,
Michael Zino

"Michael Culley" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:OM**************@tk2msftngp13.phx.gbl...
"Michael Zino" <mi******@bloombit.com> wrote in message

news:u4**************@TK2MSFTNGP12.phx.gbl...
Hello,

You should use the StringBuilder object for a Windows API call that accepts a string buffer to be modified by a method.


Why?

--
Michael Culley

Nov 18 '05 #5
Hi Michael,
String are immutable objects in managed code, and it's OK to use it as
buffers that will not be changed by the native API function.


It is possible to get a buffer back without a string builder. Will this cause problems?

[DllImport("kernel32", EntryPoint= "GetComputerNameW")]
private static extern int GetComputerName([MarshalAs(UnmanagedType.LPWStr)] string lpBuffer, ref int nSize);
private void Form1_Load(object sender, System.EventArgs e)
{
string buffer = new string('\0', 255);
int len = buffer.Length;
GetComputerName(buffer, ref len);
buffer = buffer.Substring(0, len);
MessageBox.Show(buffer);
}

--
Michael Culley
Nov 18 '05 #6
Michael,
It is possible to get a buffer back without a string builder. Will this cause problems?


Yes it can, so don't do it. To demonstrate, consider this code instead

// ---
string buffer = new string('\0', 255);
string someOtherString = buffer;

int len = buffer.Length;
GetComputerName(buffer, ref len);

Console.WriteLine(buffer);
Console.WriteLine(someOtherString);
// ---

Most people wouldn't expect someOtherString to be modified when buffer
is, because even though System.String is a reference type, its
immutability effectively gives it value semantics.

What you're doing here is basicly to break the immutability guarantee
of strings, which is a bad thing, since some runtime string
optimizations rely on the fact that a string can't change.

This may not seem so bad in this example, where all the code is
contained in a single method. But keep in mind the string interning
feature of the runtime, and the fact that completely separate pieces
of code may reference the same string, and it could have serious side
effects.
But in the original poster's case, where the parameter isn't an
LPWSTR, but rather a BSTR*, it's probably safe to pass a string. Since
the parameter is passed by ref, you can expect that the callee returns
a new BSTR (which the runtime then marshals to a new string object)
rather than to modify the existing string.

Mattias

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

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

Similar topics

1
by: Luca Arena | last post by:
Hi, I would like to know if it is possible to call the php parser from a Win32 program (e.g. a VB or C++ program), WITHOUT calling the commandline php.exe utility. I mean, is there any "php_parse"...
8
by: Patrick Useldinger | last post by:
Hi all, I am looking for beta-testers for fdups. fdups is a program to detect duplicate files on locally mounted filesystems. Files are considered equal if their content is identical,...
5
by: nbotw | last post by:
Hi, I need a function call in my xml web service. I have an old win32 dll and i have created a class to have my declaration in. Imports System.Runtime.InteropServices Public Class Win32
15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
1
by: Fender Mussel | last post by:
Hi, We are in a situation where we have a Win32 application which is currently deployed to about 15000 desktop machines and want to rewrite the back end (business logic) in a modern language. At...
1
by: Wilfried Mestdagh | last post by:
Hi, I have a few related questios: 1. if I pass a string to win32 api (or other dll not written in .net), is it possible GC cleans up while function is still busy ? eg: public class Win32...
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
0
by: Scott Gunn | last post by:
Hello I'm trying to scroll a region of graphics, the best way I can see is to use the ScrollWindowEx API because it is designed for doing this However it doesn't work correctly, Why? Here...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
3
by: Vinz | last post by:
Hello everyone, With no C# nor C++ experience I wanted to make a C# WinForms client calling a C++ Win32 DLL. All day long I have been scraping knowledge from webpages and the C++ pocketreference...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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...

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.