C#非托管函数调用,可能的数据丢失,Ascii Vs. Unicode,中文文件夹名称

时间:2011-09-16 20:28:10

标签: c# unicode localization dllimport cjk

所以这是交易,我正在使用一个C#应用程序调用一个遗留的C ++ DLL,它反过来循环通过一个目录拉回某些目录的名称,即具有.lib的目录,我有一个目录以下3个文件夹:Default.Lib,中文文本帧的文件.lib,我们的.lib。

正如您所看到的,我们有一些中文文件夹名称,c ++代码在内存中构建了一个字符串,如下所示,它使用strcat在内存中构建它。但是当控制权返回到c#代码时,它会显示部分数据丢失,而剩下的只有两个文件夹是前两个。 Default.Lib,中文文本帧的文件.lib,与我们的.lib的东西在翻译中丢失了,我将非常感谢任何人可能有的见解。感谢。

C#代码段

 lock (padLock)
            {
                ConnectSign(service);

                int size = MaxFileListSize * 100;
                byte[] mem = new byte[size];
                string finalList;
                int used = size;
                int fileCount = 0;
                string library = "*";
                string extension = "*";
                V7_FILE_LIST_TYPE type = V7_FILE_LIST_TYPE.LibraryList;

                fixed (byte* listbytes = mem)
                {
                    int error = NativeMethods.GetFileDirInfo(sign, type, fileServer, library, extension, &fileCount, listbytes, &used);
                    if (error != 0)
                        throw new V7ResponseException(error, sign, service, "GetFileDirInfo");
                }

                finalList = Encoding.Default.GetString(mem, 0, (int)used);

                string[] libraryArray = finalList.Split(new char[] { '\n', '\0' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < libraryArray.Length; i++)
                {
                    int index = libraryArray[i].LastIndexOf(".lib", StringComparison.OrdinalIgnoreCase);
                    if (index > 0)
                        libraryArray[i] = libraryArray[i].Substring(0, index);
                    //libraryArray[i] = libraryArray[i].Trim().ToLower(CultureInfo.CurrentCulture).Replace(".lib", string.Empty);
                }

                return libraryArray;
            }

[DllImport("V7SSRpc.dll", CharSet = CharSet.Ansi, EntryPoint = "V7ssGetFileDirInfo", BestFitMapping = false, ThrowOnUnmappableChar = true)]
            public static extern int GetFileDirInfo(string sign, V7_FILE_LIST_TYPE type, string fileServer, string library, string extension, int* fileCount, byte* files, int* bytesUsed);

enter image description here ***************************** C ++ DLL代码------------------ --------------------

   //--------------------------------------------------------------------
    // RETURN :
    //
    // PARAMS : eListType
    //              szServer
    //              szLib
    //              szExt
    //              *pdwFileCnt
    //              *pbyFileBuf
    //              *pdwFileBufSize
    //
    // REMARKS:
    //
    BOOL CVSign::apiGetFileDirInfo(V7_FILE_LIST_TYPE eListType, LPCSTR szServer, LPCSTR szLib, LPCSTR szExt, 
                                             DWORD *pdwFileCnt, char *pbyFileBuf, DWORD *pdwFileBufSize) const
    {
        BOOL bReturn=TRUE;
        CString sServer(szServer);
        CString sLib(szLib);
        CString sExt(szExt);
        CString sFileInfo, sTemp;
        CStringArray asFiles;
        CFileStatus status;
        CV7Files V7Files;
        DWORD dwBufUsed=0;

    // SOME OTHER LOGIC (not posted)

        USES_CONVERSION;

        //CoInitialize(NULL);
        //AVIFileInit();

        CString sFilePath;
        CV7SequenceFile V7Seq;

        CV7FileInfo fileInfo;

        // go through list of files and build the buffer with file names and other info
        for (nFile=0; nFile<nFiles; nFile++)
        {
            // MORE OBSCURED LOGIC

            sFileInfo += _T("\n");

            // add file info to buffer
            int nLen = sFileInfo.GetLength();
            if (dwBufUsed+nLen<*pdwFileBufSize)
            {
                strcat(pbyFileBuf, T2CA(sFileInfo)); //<--- THIS IS THE IMPORTANT PART
                int nTemp = sFileInfo.GetLength();
                dwBufUsed += nTemp;
            }
            else
            {
                *pdwFileBufSize = 0;

                AVIFileExit();
                CoUninitialize();

                return FALSE;
            }
        }   // end for files

        //AVIFileExit();
        //CoUninitialize();

        *pdwFileBufSize = dwBufUsed;

        return bReturn;
    } // end apiGetFileDirInfo()

enter image description here

1 个答案:

答案 0 :(得分:0)

我怀疑问题是您指定Charset=CharSet.Ansi,因此默认的编组行为是将返回的字符串转换为ANSI。这会引起问题。

您可能希望指定字符串Charset=CharSet.Unicode,并可能为某些字符串指定自定义编组。有关如何更改单个参数的字符串编组行为的信息,请参阅http://msdn.microsoft.com/en-us/library/s9ts558h.aspx#cpcondefaultmarshalingforstringsanchor5