473,324 Members | 2,166 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,324 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 2674

"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
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.