[英]Porting C++ Structure into C# (from an Unmanaged dll)
我是一名汽車工程師,我的公司決定購買一種僅使用C ++的API的硬件(我得到了未處理的dll和頭文件)。 問題是我只用C#編碼,並且我們的大多數應用程序都非常易於構建,因為硬件提供程序始終以C#(托管dll)的形式提供API。 現在,我需要將未管理的dll中的所有函數轉換為C#函數。 一直很順利,直到我遇到
typedef struct can_msg
{
unsigned short ide; // Standard/extended msg
unsigned int id; // 11 or 29 bit msg id
unsigned short dlc; // Size of data
unsigned char data[CAN_MSG_DATA_LEN]; // Message pay load
unsigned short rtr; // RTR message
} can_msg_t;
我不知道如何使用它,因為這種結構是函數的一個參數,例如:
VTC1010_CAN_BUS_API int CAN_Transmission(can_msg_t *msg);
請幫助大家。 不要因為我缺乏知識而羞辱我。 我試圖找到,但這對我來說太難了。
假設您不了解如何在.net托管應用程序中使用未損壞的dll,我可以告訴您一些非常基本的知識。 可行的方法是使用Visual C ++為非托管類創建“包裝器”,然后可以使用此包裝器中定義的類對非托管代碼進行操作。
您可以在這里找到一個很好的教程: http : //www.codeproject.com/Articles/14180/Using-Unmanaged-C-Libraries-DLLs-in-NET-Applicatio
抱歉,請不要更具體,但是在創建代碼之前,您需要開始學習。 祝好運!
這是通過P / Invoke Interop Assistant篩子進行的快速而骯臟的轉換。
輸入代碼:
#define CAN_MSG_DATA_LEN 100 // adjust correct value here
typedef struct can_msg
{
unsigned short ide; // Standard/extended msg
unsigned int id; // 11 or 29 bit msg id
unsigned short dlc; // Size of data
unsigned char data[CAN_MSG_DATA_LEN]; // Message pay load
unsigned short rtr; // RTR message
} can_msg_t;
int CAN_Transmission(can_msg_t *msg);
輸出代碼:
public partial class NativeConstants {
/// CAN_MSG_DATA_LEN -> 100
public const int CAN_MSG_DATA_LEN = 100;
}
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
public struct can_msg {
/// unsigned short
public ushort ide;
/// unsigned int
public uint id;
/// unsigned short
public ushort dlc;
/// unsigned char[100]
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=100)]
public string data;
/// unsigned short
public ushort rtr;
}
public partial class NativeMethods {
/// Return Type: int
///msg: can_msg_t*
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="CAN_Transmission")]
public static extern int CAN_Transmission(ref can_msg msg) ;
}
static class NativeMethods
{
// To load dll
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
// To get the Address of the Function
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
// Freeing up the Library for other usage.
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
}
class manageCAN
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
// Declaration of function to get
private delegate int CAN_Initial(int baudRate);
private delegate int Library_Release();
// Getting the String for .dll Address
static readonly string dllfile = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\VTC1010_CAN_Bus.dll";
public int IntialiseCAN (int baudrate)
{
// Loading dll using Native Methods
IntPtr pDll = NativeMethods.LoadLibrary(dllfile);
if (pDll== IntPtr.Zero)
{
MessageBox.Show("Loading Failed");
}
// Getting the Adress method
IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(pDll, "CAN_Initial");
CAN_Initial initialiseCAN = (CAN_Initial)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(CAN_Initial));
int result = initialiseCAN(baudrate);
bool iresult = NativeMethods.FreeLibrary(pDll);
return result;
}
}
它有效,但是當我有一個結構作為參數時,我感到困惑,這就是為什么我問這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.