我的Strcuture
struct Address{
char Name[4096];
char Address[4096];
char Description[1024];
} ;
和Strcuture的Typedef
typedef struct Address* (*__cdecl pgetAdd) ();
我将值传递给类型
pgetAdd getStat = (pgetAdd)GetProcAddress(HMODULE (hGetProcIDDLL),"getStat");
char *Argv[] = { "Karthik", "23", "Chennai", "04-04-1984"};
getStat = (pgetAdd) startTest(Argv);
Address *mydata;
printf("\n mydata\n %s ", mydata>Name);
printf("\n mydata\n %s ", mydata>Address);
printf("\n mydata\n %s ", mydata> Description);
这包含c ++ dll
我需要从c#
调用dll我尝试了
我的Strcuture
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Address
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4096)]
public string Name;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4096)]
public string Address;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string Description;
}
我像这样使用Typedef
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Auto)]
public delegate IntPtr getStatDelegate();
我像这样导入dll
[DllImport(@"E:\\EmployeeData.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "startTest", CharSet = CharSet.Ansi)]
public unsafe static extern IntPtr startTest ([In] IntPtr argv);
我的代码是
IntPtr args= StringArrayToIntPtr<byte>(new string[] { "Karthik", "23", "Chennai", "04-04-1984" });
IntPtr ptr = startTest(args);
getStatDelegate getStat = (getStatDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(getStatDelegate));
IntPtr structPtr = new IntPtr();
structPtr= getStat();
Address data = (Address)Marshal.PtrToStructure(structPtr, typeof(Address));
richTextBox1.Text = data.Name;
richTextBox2.Text = data.Address;
richTextBox3.Text = data.Description;
public static IntPtr StringArrayToIntPtr<GenChar>(string[]
InputStrArray) where GenChar : struct
{
int size = InputStrArray.Length;
IntPtr[] InPointers = new IntPtr[size];
int dim = IntPtr.Size * size;
IntPtr rRoot = Marshal.AllocCoTaskMem(dim);
for (int i = 0; i < size; i++)
{
if (typeof(GenChar) == typeof(char))
{
InPointers[i] = Marshal.StringToCoTaskMemUni(InputStrArray[i]);
}
else if (typeof(GenChar) == typeof(byte))
{
InPointers[i] = Marshal.StringToCoTaskMemAnsi(InputStrArray[i]);
}
}
Marshal.Copy(InPointers, 0, rRoot, size);
return rRoot;
}
我的输出
richTextBox1.Text = data.Name= Karthik;
richTextBox2.Text = data.Address= chennai;
richTextBox3.Text = data.Description=Welcome to our team something in dll;
我没有得到输出请更正代码以及帮助找到解决方案
提前感谢天才
**