0

我想在 C# 应用程序中使用用 C 编写的函数。该函数被导出为 dll 文件。C函数代码:

    #include <stdio.h>
    #include <stdlib.h>
    extern "C" __declspec(dllexport) char * Encrypt(char plainText[],int height,long inputLength)
    {
        char *encryptedText=(char*)malloc(sizeof(plainText));
        char **cipherArray;


        cipherArray=(char**)malloc(height*sizeof(char *));
        for(long i=0; i<height; i++)
        {
            cipherArray[i]=(char*)malloc(inputLength*sizeof(char));
            for (long j=0; j<inputLength ; j++)
                cipherArray[i][j]='#';
        }






bool addRow=true;
long row=0;
long column = 0;
long arrayIterator = 0;
while(arrayIterator<inputLength){
    cipherArray[row][column] = plainText[arrayIterator];

            column++;
            if(addRow)row++;
            else row--;
            if (row >= height)
            {
                row--;
                row--;
                addRow=false;
            }
            else if (row < 0)
            {
                row++;
                row++;
                addRow = true;
            }
            arrayIterator++;
}



long iterator=0;
for (long i=0; i< height; i++)
    for(long j=0; j<inputLength;j++){
        if(cipherArray[i][j]!='#'){
            encryptedText[iterator]=cipherArray[i][j];
            iterator++;
        }
    }

for(int i=0; i<height; i++)
    free(cipherArray[i]);
free(cipherArray);
cipherArray = NULL; 

return encryptedText;
    }

在 C# 应用程序中,我创建了使用此函数的类:

namespace RailFenceCipher
{
public class CCipher
   {
     [DllImport("CCipher.dll", CharSet = CharSet.Unicode)]
        unsafe public static extern char* Encrypt(char[] plainText, int height, long inputLength);

    }
}

现在是困难的部分...... C 函数返回 char* 对象(char 数组)。我的问题是如何将函数的结果分配给 C# 字符串?

到目前为止,我一直在尝试自己管理它,但它仍然不起作用......

调用dll函数:

private void cipherC()
        {
            this.fileOutputC = "";
            unsafe
            {
                string cEncrypted =  new string(((CCipher.Encrypt(this.fileInput.ToCharArray(), this.height,fileInput.Length))));
             this.fileOutputC = cEncrypted;
            }
     }

提前感谢您的帮助!

编辑:

我已经像这样重新组织了我的代码:

dll中的C代码:

    struct Wrapper{
        unsigned char* plainText;
        int height;
        long inputLength;
        unsigned char* result;
    };
    void Encrypt(unsigned char plainText[],int height,long inputLength,unsigned char result[])
    {
        unsigned char *encryptedText=(unsigned char*)malloc(sizeof(plainText));
        unsigned char **cipherArray;


        cipherArray=(unsigned char**)malloc(height*sizeof(unsigned char *));
        for(long i=0; i<height; i++)
        {
            cipherArray[i]=(unsigned char*)malloc(inputLength*sizeof(char));
            for (long j=0; j<inputLength ; j++)
                cipherArray[i][j]='#';
        }






        bool addRow=true;
        long row=0;
        long column = 0;
        long arrayIterator = 0;
        while(arrayIterator<inputLength){
            cipherArray[row][column] = plainText[arrayIterator];

                    column++;
                    if(addRow)row++;
                    else row--;
                    if (row >= height)
                    {
                        row--;
                        row--;
                        addRow=false;
                    }
                    else if (row < 0)
                    {
                        row++;
                        row++;
                        addRow = true;
                    }
                    arrayIterator++;
        }



        long iterator=0;
        for (long i=0; i< height; i++)
            for(long j=0; j<inputLength;j++){
                if(cipherArray[i][j]!='#'){
                    encryptedText[iterator]=cipherArray[i][j];
                    iterator++;
                }
            }

        long j=0;
        while(j<inputLength){
            result[j]=encryptedText[j];
            j++;
        }

        for(long i=0; i<height; i++)
            free(cipherArray[i]);
        free(cipherArray);
        cipherArray = NULL; 


    }

    extern "C" __declspec(dllexport) void CreateCWrapper(struct Wrapper **myWrapper,string plainText, long height, string result,long inputLength){


        *myWrapper=(struct Wrapper*)malloc(sizeof(struct Wrapper));
        (*myWrapper)->height = height;
        (*myWrapper)->inputLength= inputLength;
        (*myWrapper)->plainText= (unsigned char*)malloc(inputLength * sizeof(char));
         if ((*myWrapper)->plainText != NULL)
          for (long i = 0; i < inputLength; ++i)
              (*myWrapper)->plainText[i] = plainText[i];
        (*myWrapper)->result= (unsigned char*)malloc(inputLength * sizeof(char));



    }


    extern "C" __declspec(dllexport) void EncryptWithWrapper(Wrapper *myWrapper){
        unsigned char *table=(unsigned char*)malloc(sizeof(char)*myWrapper->inputLength);
        for(long i=0;i<myWrapper->inputLength;i++){
            table[i]=myWrapper->plainText[i];
        }
        unsigned char *res=(unsigned char*)malloc(sizeof(char)*myWrapper->inputLength);
        Encrypt(table,myWrapper->height,myWrapper->inputLength,res);
        for(long i=0;i<myWrapper->inputLength;i++){
            myWrapper->result[i]=res[i];

        }
        free(table);
        free(res);
    }

    extern "C" __declspec(dllexport) string getStrResultFromWrapper(Wrapper *myWrapper){
        string stringres;
        for(long i=0;i<myWrapper->inputLength;i++){
            stringres+=myWrapper->result[i];
        }
        return stringres;
    }

C#类:

    public class CCipher
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct Wrapper
            {
                public IntPtr plainText;
                public int height;
                public IntPtr inputLength;
                public string result;
            }

            [DllImport("CCipher.dll")]
            public static extern void CreateCWrapper(out IntPtr myWrapper, string plainText, long height, string result, long inputLength);

            [DllImport("CCipher.dll")]
            public static extern void EncryptWithWrapper(IntPtr myWrapper);

            [DllImport("CCipher.dll")]
            public static extern string getStrResultFromWrapper(IntPtr myWrapper);
       }

和调用函数:

       private void cipherC()
               {

                   IntPtr myWrapper;
                   CCipher.CreateCWrapper(out myWrapper, this.fileInput, this.height, this.fileOutputC, this.fileInput.Length);
                   CCipher.EncryptWithWrapper(myWrapper);

                   CCipher.Wrapper dataWrapper=(CCipher.Wrapper) Marshal.PtrToStructure(myWrapper,typeof(CCipher.Wrapper));
               }

问题是调用时:

   CCipher.CreateCWrapper(out myWrapper, this.fileInput, this.height, this.fileOutputC, this.fileInput.Length);

该应用程序刚刚停止。有什么提示吗?

4

0 回答 0