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

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

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);
Nov 16 '05 #1
9 2678

"John Price" <jo********@vbiretail.com> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
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.
Nov 16 '05 #2
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...
"John Price" <jo********@vbiretail.com> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
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);

Nov 16 '05 #3
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

Nov 16 '05 #4
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.
"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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]
--
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

Nov 16 '05 #5
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 [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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]
--
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

Nov 16 '05 #6
Hi,

"John Price" <jo********@vbiretail.com> wrote in message
news:e3****************@TK2MSFTNGP11.phx.gbl...
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

"Jackson Davis [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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]
--
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


Nov 16 '05 #7
Why not simply use, Marshal.PtrToStringUni, like this:
IntPtr unmPtr = <ptr to unicode char buffer...>
string s = Marshal.PtrToStringUni(unmPtr);

Willy.
"John Price" <jo********@vbiretail.com> wrote in message
news:e3****************@TK2MSFTNGP11.phx.gbl...
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 [MSFT]" <an*******@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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]
--
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


Nov 16 '05 #8
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #9
> 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.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #10

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

Similar topics

11
by: John Price | last post by:
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.