lstrcpyW: Work Vb.NEt, but not C#. Why?

  • Thread starter Thread starter John Price
  • Start date Start date
J

John Price

Any idea why the follwing API call works fine under VB.NET, but not under
c#?

I've spent all day trying and its driving me up the wall!.



Thanks



Jp.





[DllImport("kernel32", SetLastError=true)] static extern int lstrcpyW (
string lpString1, string lpString2);


string x="123456";

string y="654321";

lstrcpyW(x,y);
 
John Price said:
Any idea why the follwing API call works fine under VB.NET, but not under
c#?

I've spent all day trying and its driving me up the wall!.



Thanks



Jp.





[DllImport("kernel32", SetLastError=true)] static extern int lstrcpyW (
string lpString1, string lpString2);


string x="123456";

string y="654321";

lstrcpyW(x,y);
Not sure why you call an unmanaged function to copy a managed string to a
managed string, but in C# you need to pass a reference to the string
references.

[DllImport("kernel32", SetLastError=true)] static extern int lstrcpyW (
ref string lpString1, ref string lpString2);
....
int i = lstrcpyW(ref x,ref y);

Willy.


Willy.
 
Ok, so if you set the two strings to references in the definition it works.

Now, what I'm actually tring to do is get a string from its pointer. The
second parameter can be a pointer. So this is an int?

I cant get this to work...
 
PInvoke can be tricky if you don't have the types correct. Since you didn't say what errors you are seeing, I can't say what is wrong. However, here is an application in C# that works correctly

using System

namespace ConsoleApplication

/// <summary
/// Summary description for Class1
/// </summary
class Class

[System.Runtime.InteropServices.DllImport("kernel32", SetLastError=true)]
static extern System.String lstrcpyW (System.String lpString1, System.String lpString2)

/// <summary
/// The main entry point for the application
/// </summary
[STAThread
static void Main(string[] args

System.String x = ""
System.String y = "654321"
x = Class1.lstrcpyW(x, y)
System.Console.WriteLine(x)
System.Console.WriteLine(y)




If your interested in PInvoke and COM Interop, there is a great book written by Adam Nathan called ".Net and COM: the complete interoperability guide" that explains what types to use in creating conversions etc... A lot of developers call it the "interop bible"

Jackson Davis [MSFT
-
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
But your PInvoke declaration is incorrect, the string args must be passed by
reference.

just try calling:

Class1.lstrcpyW(x, y);
and watch the output of
System.Console.WriteLine(x);

It works when calling x = Class1.lstrcpyW(x, y); ...
because the function returns a marshaled buffer in x.

Willy.
 
What I have is a pointer to a string. I want to move the string itsself to a
string variable. I am not seeing any 'errors' as such. It just doesnt do it.

However, If I use declare function in a vb dll and reference it from C# it
works fine. So its down to mismatched reference types, I just cant see
where.

The C#
[DllImport("Kernel32", EntryPoint="lstrcpyW")] private static extern int
lstrcpyW (ref string lpszDest,System.Int32 lpszSrc);

The Vb
Private Declare Function lstrcpyW Lib "KERNEL32" (ByVal lpszDest As String,
ByVal lpszSrc As System.Int32) As Integer



Jackson Davis said:
PInvoke can be tricky if you don't have the types correct. Since you
didn't say what errors you are seeing, I can't say what is wrong. However,
here is an application in C# that works correctly:
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError=true)]
static extern System.String lstrcpyW (System.String lpString1, System.String lpString2);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.String x = "";
System.String y = "654321";
x = Class1.lstrcpyW(x, y);
System.Console.WriteLine(x);
System.Console.WriteLine(y);
}
}
}

If your interested in PInvoke and COM Interop, there is a great book
written by Adam Nathan called ".Net and COM: the complete interoperability
guide" that explains what types to use in creating conversions etc... A lot
of developers call it the "interop bible".
Jackson Davis [MSFT]
 
Hi,

John Price said:
What I have is a pointer to a string. I want to move the string itsself to a
string variable. I am not seeing any 'errors' as such. It just doesnt do it.

However, If I use declare function in a vb dll and reference it from C# it
works fine. So its down to mismatched reference types, I just cant see
where.

The C#
[DllImport("Kernel32", EntryPoint="lstrcpyW")] private static extern int
lstrcpyW (ref string lpszDest,System.Int32 lpszSrc);

The signature should be
[DllImport("Kernel32", EntryPoint="lstrcpyW")] private static extern int
lstrcpyW (ref string lpszDest, IntPtr lpszSrc);
or
[DllImport("Kernel32", EntryPoint="lstrcpyW")] private static extern int
lstrcpyW (StringBuilder lpszDest, IntPtr lpszSrc);

Both cases are valid, altough there is a difference.
In the first case the framework will :
- copy dest string to an unmanaged buffer
- execute unmanaged function passing pointer to this unmanaged buffer
- copy unmanaged buffer into a new string

In second case the unmanaged function gets a direct pointer to the internal
string in stringbuilder. The second case is most widely used.

Keep in mind that in both cases the dest string should contain at least as
many chars as the source _before_ calling the function. For stringbuilder
this can be done with the capacity property.

And you might want to see if Marshal.PtrToStringUni can help you.

You can cast an int to an IntPtr.

HTH
greetings


The Vb
Private Declare Function lstrcpyW Lib "KERNEL32" (ByVal lpszDest As String,
ByVal lpszSrc As System.Int32) As Integer




didn't say what errors you are seeing, I can't say what is wrong. However,
here is an application in C# that works correctly:
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError=true)]
static extern System.String lstrcpyW (System.String lpString1, System.String lpString2);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.String x = "";
System.String y = "654321";
x = Class1.lstrcpyW(x, y);
System.Console.WriteLine(x);
System.Console.WriteLine(y);
}
}
}

If your interested in PInvoke and COM Interop, there is a great book
written by Adam Nathan called ".Net and COM: the complete interoperability
guide" that explains what types to use in creating conversions etc... A lot
of developers call it the "interop bible".
Jackson Davis [MSFT]
 
Why not simply use, Marshal.PtrToStringUni, like this:
IntPtr unmPtr = <ptr to unicode char buffer...>
string s = Marshal.PtrToStringUni(unmPtr);

Willy.


John Price said:
What I have is a pointer to a string. I want to move the string itsself to
a
string variable. I am not seeing any 'errors' as such. It just doesnt do
it.

However, If I use declare function in a vb dll and reference it from C# it
works fine. So its down to mismatched reference types, I just cant see
where.

The C#
[DllImport("Kernel32", EntryPoint="lstrcpyW")] private static extern int
lstrcpyW (ref string lpszDest,System.Int32 lpszSrc);

The Vb
Private Declare Function lstrcpyW Lib "KERNEL32" (ByVal lpszDest As
String,
ByVal lpszSrc As System.Int32) As Integer



Jackson Davis said:
PInvoke can be tricky if you don't have the types correct. Since you
didn't say what errors you are seeing, I can't say what is wrong. However,
here is an application in C# that works correctly:
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
[System.Runtime.InteropServices.DllImport("kernel32", SetLastError=true)]
static extern System.String lstrcpyW (System.String lpString1, System.String lpString2);

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.String x = "";
System.String y = "654321";
x = Class1.lstrcpyW(x, y);
System.Console.WriteLine(x);
System.Console.WriteLine(y);
}
}
}

If your interested in PInvoke and COM Interop, there is a great book
written by Adam Nathan called ".Net and COM: the complete interoperability
guide" that explains what types to use in creating conversions etc... A
lot
of developers call it the "interop bible".
Jackson Davis [MSFT]
 
Any idea why the follwing API call works fine under VB.NET, but not under
c#?

I've spent all day trying and its driving me up the wall!.
[DllImport("kernel32", SetLastError=true)] static extern int lstrcpyW (
string lpString1, string lpString2);


string x="123456";

string y="654321";

lstrcpyW(x,y);


You must pass a StringBuilder for the target string, not a string since
string is inmutable.
Anyways, why do you need this API call? StringBuilder already has a rich set
of methods for manipulating strings which internally just calls the API
functions so it won't be much slower that native code.
 
Back
Top