如何在c#中运行时编译c ++代码

时间:2013-10-07 18:46:27

标签: c# c++ dll runtime wrapper

我想在运行时使用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;

}

0 个答案:

没有答案