簡體   English   中英

如何將包含文件路徑的字符串從 C# 傳遞給需要 PChar 參數的 Pascal DLL 中的函數?

[英]How to pass a string containing a file path from C# to a function in a Pascal DLL requiring a PChar argument?

如何將包含文件路徑的字符串從 C# 傳遞給需要 PChar 參數的 Pascal DLL 中的函數?

Pascal DLL 有一個“FileExists”調用,用於檢查參數 (AFile: PChar) 是否作為文件存在。 我已經成功導入了 dll,如下所示:

[DllImport(pcsm,
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Unicode,
    EntryPoint = "PCS")]

並聲明函數如下:

public static extern int PCSM([MarshalAs(UnmanagedType.BStr)] string AFile);

然后在帕斯卡:

function PCS(AFile: PChar): PChar; stdcall;

var XD: IXMLDocument;
Race: TPCSRace;

begin

try
     if not FileExists(AFile) then
        raise EFOpenError.CreateFmt('File "%s" not found', [AFile]);
     else
         //do something with AFile...

end

但是當我這樣調用函數時:

pascalPath = "path\\to\\the\\file";
PCSM(pascalPath);

Dll 不對文件進行操作(必須對其進行編輯並且它是一個 xml 文件)。

dll是某組織官方提供的,無法編輯,我已經減少了代碼,但是dll是正確的。

解決了

    [DllImport(pcsm,
        CallingConvention = CallingConvention.Winapi,
        CharSet = CharSet.Ansi,
        EntryPoint = "PCS")]
    public static extern int PCS(string AFile);

進而

        StringBuilder pascalPath = new StringBuilder(xmlPath, xmlPath.Length);

        int result = PCS(pascalPath.ToString());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM