Hello
I have an old c++ dll (trdp.dll) inclusive .lib file.
With an Visual Studio 2005 C++ Project, I can use it implicit.
Now, I want to use the dll with a C# Project.
It doesn`t work.
I tryed two ways, calling the function (TrdpCreateLink(...))
1. Using C# conform Variables as parameters for the dll functions
2. Using "unsafe" and "fixed" functionality
Both times the same error occurs:
The function will not be executed and the Last Error = 1008
I don`t know what`s that kind of error and how to avoid it.
I added the code below.
Thanks for any help!
Michael
Function declaration in c++ lib:
[highlight=cpp]
extern "C" _declspec(dllexport) unsigned long far TrdpCreateLink (char* pszRemoteIPAddress,
unsigned short wRemotePortNum, unsigned short wLocalPortNum, unsigned long dwTimeout,
unsigned short* pLinkID);
[/highlight]
//1. try(conform variables):
...
//2. Try(unsafe, fixed):
...
I have an old c++ dll (trdp.dll) inclusive .lib file.
With an Visual Studio 2005 C++ Project, I can use it implicit.
Now, I want to use the dll with a C# Project.
It doesn`t work.
I tryed two ways, calling the function (TrdpCreateLink(...))
1. Using C# conform Variables as parameters for the dll functions
2. Using "unsafe" and "fixed" functionality
Both times the same error occurs:
The function will not be executed and the Last Error = 1008
I don`t know what`s that kind of error and how to avoid it.
I added the code below.
Thanks for any help!
Michael
Function declaration in c++ lib:
[highlight=cpp]
extern "C" _declspec(dllexport) unsigned long far TrdpCreateLink (char* pszRemoteIPAddress,
unsigned short wRemotePortNum, unsigned short wLocalPortNum, unsigned long dwTimeout,
unsigned short* pLinkID);
[/highlight]
//1. try(conform variables):
...
C#:
using System.Runtime.InteropServices;
namespace DllCall
{
public partial class Form1 : Form
{
[DllImport("trdp.dll", SetLastError = true)]
public static extern ulong TrdpCreateLink(StringBuilder pszRemoteIPAddress, ushort wRemotePortNum, ushort wLocalPortNum, ulong dwTimeout, ref ushort pLinkID);
...
private void button1_Click(object sender, EventArgs e)
{
StringBuilder lpszServerName = new StringBuilder("127.0.0.1");
ushort LinkId = 0;
ulong res = TrdpCreateLink(lpszServerName, 5001, 5002, 4294967295, ref LinkId);
int LastErr = Marshal.GetLastWin32Error();
// LastErr == 1008
}
}
}
//2. Try(unsafe, fixed):
...
C#:
using System.Runtime.InteropServices;
namespace DllCall
{
public partial class Form1 : Form
{
[DllImport("trdp.dll", SetLastError = true)]
public static unsafe extern ulong TrdpCreateLink(char* pszRemoteIPAddress, ushort wRemotePortNum, ushort wLocalPortNum, ulong dwTimeout, ushort* pLinkID);
...
private void button1_Click(object sender, EventArgs e)
{
unsafe
{
String str = "127.0.0.1";
char[] ServerName = new char[20];
str.CopyTo(0, ServerName, 0, 9);
ushort b = 0;
ushort* LinkId = &b;
fixed (char* lpszServerName = ServerName)
{
ulong res = TrdpCreateLink(lpszServerName, 5001, 5002, 4294967295, LinkId);
}
}
int LastErr = Marshal.GetLastWin32Error();
// LastErr == 1008
}
}
Last edited by a moderator: