我想在运行时使用 c# 将一些 c++ 代码编译为 dll。那可能吗?我该如何进行?是否可以简单地将 c++ 代码转换为 c#?我是 C++ 新手,甚至不知道它是否可能。dll用于其他应用程序,因此dll的输入和输出是固定的,不能更改。
我一直在看 cppcodeprovider,但是,我似乎没有工作。
是否有可能为 c# 构建一个 c++ 包装器?
这是我想在运行时编译的 c++ 代码:
#include <stdio.h>
#include <string.h>
#define NINT(a) ((a) >= 0.0 ? (int)((a)+0.5) : (int)((a)-0.5))
extern "C" //avoid mangled names
{ void __declspec(dllexport) __cdecl cDISCON(float *avrSwap, int *aviFail, char *accInfile, char *avcOutname, char *avcMsg);
}
//Main DLL routine
void __declspec(dllexport) __cdecl cDISCON(float *avrSwap, int *aviFail, char *accInfile, char *avcOutname, char *avcMsg)
{
char Message[257], InFile[257], OutName[1025];
float rTime, rMeasuredSpeed, rMeasuredPitch;
int iStatus, iFirstLog;
static float rPitchDemand;
//Take local copies of strings
memcpy(InFile,accInfile, NINT(avrSwap[49]));
InFile[NINT(avrSwap[49])+1] = '\0';
memcpy(OutName,avcOutname, NINT(avrSwap[50]));
OutName[NINT(avrSwap[50])+1] = '\0';
//Set message to blank
memset(Message,' ',257);
//Set constants
SetParams(avrSwap);
//Load variables from Bladed (See Appendix A)
iStatus = NINT (avrSwap[0]);
rTime = avrSwap[1];
rMeasuredPitch = avrSwap[3];
rMeasuredSpeed = avrSwap[19];
//Read any External Controller Parameters specified in the User Interface
if (iStatus == 0)
{
*aviFail = ReadData(InFile, Message); //User to supply this routine
rPitchDemand = rMeasuredPitch; //Initialise
}
//Set return values using previous demand if a sample delay is required
avrSwap[44] = rPitchDemand;
//Main calculation //User to supply calcs routine
if (iStatus >= 0 && *aviFail >= 0)
*aviFail = calcs(iStatus, rMeasuredSpeed, rMeasuredPitch, &rPitchDemand, OutName, Message);
//Logging output - example
avrSwap[64] = 2; //No of outputs
iFirstLog = NINT(avrSwap[62])-1; //Address of first output
strcpy(OutName, "Speed:A/T;Pitch:A"); //Names and units
avrSwap[iFirstLog] = rMeasuredSpeed; //First Value
avrSwap[iFirstLog+1] = rMeasuredPitch; //Second value
//Return strings
memcpy(avcOutname,OutName, NINT(avrSwap[63]));
memcpy(avcMsg,Message,MIN(256,NINT(avrSwap[48])));
return;
}