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".