[英]How should I import Powerflex functions for use in C#?
我已獲得在Visual Studio 2010 C#中使用pfxlibn.dll的任務, 據我所知 ,這是一個Powerflex庫。 http://pfxcorp.com/clib-fns.htm
我嘗試了一些導入PclGetField
函數的變體,例如:
[DllImport("pfxlibn.dll", SetLastError = true)]
public static extern void PclGetField(
Int32 iHandle,
Int32 iFieldNum,
[MarshalAs(UnmanagedType.LPStr)] string Date
);
[DllImport("pfxlibn.dll",
CallingConvention = CallingConvention.StdCall,
CharSet = CharSet.Ansi,
EntryPoint = "PclGetField")]
public static extern int PclGetField(Int32 iHandle, Int32 iFieldNum, string Data)
[DllImport("pfxlibn.dll", EntryPoint = "PclGetField",
ExactSpelling = true,
CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl)]
public static extern Int32 PclGetField(Int32 iHandle, Int32 iFieldNum,[MarshalAs(UnmanagedType.VBByRefStr)] ref string input);
到目前為止,以上方法均無效。 還是以上的變化。
在Delphi中,代碼看起來像
pTmpBuf : PChar;
iLastErr := PclGetField(fHandle,iField,pTmpBuf);
您鏈接到的文檔非常稀少,因此要回答我們需要猜測的問題。
您需要的功能如下:
aResult PCLENTRY PclGetField (aDHandle fdh, anInt eno, aString data);
//Retrieves the formatted value of a field from a file buffer.
aResult PCLENTRY PclGetFieldLen (aDHandle fdh, anInt eno, anInt *length);
//Gets the length of a field.
為了確定如何處理此問題,我們需要更多信息:
PCLENTRY
宏會評估什么? 我將假設__stdcall
,但是您將需要檢查頭文件。 aResult
? 我將假定為int
但您需要檢查頭文件。 aDHandle
? 我將假定為void*
或INT_PTR
但您需要檢查頭文件。 anInt
? 我將假定為int
但您需要檢查頭文件。 您將需要調用PclGetFieldLen
來找出所需的緩沖區大小。 然后,您將分配該緩沖區並調用PclGetField
進行填充。 您將需要找出PclGetFieldLen
返回的值是否包含零終止符。 我假設事實並非如此。
基於這些假設,您可以這樣編寫pinvoke:
[DllImport("pfxlibn.dll", CallingConvention=CallingConvention.Stdcall)]
public static extern int PclGetFieldLen(fdh IntPtr, int eno, out int length);
[DllImport("pfxlibn.dll", CallingConvention=CallingConvention.Stdcall,
CharSet=CharSet.Ansi)]
public static extern int PclGetField(fdh IntPtr, int eno, StringBuilder data);
然后,您可以像下面這樣調用函數:
IntPtr fdh = .... // whatever function creates the database handle
int eno = .... // get the field number from somewhere
int length;
int res = PclGetFieldLen(fdh, eno, out length);
//check res for errors
StringBuilder data = new StringBuilder(length);
res = PclGetFieldLen(fdh, eno, data);
string field = data.ToString();
由於我們不了解所有詳細信息,因此此答案中有一堆未知數。 您具有頭文件,並且可以與圖書館供應商聯系以解決未知問題。 但希望上面的概述告訴您需要回答什么問題。
感謝David Heffernan,但是,我沒有提供足夠的信息或沒有足夠研究delphi代碼。
缺少的主要項目是PclClear,PclPutField和PclFind。 此后,我發現了以下作品:
[DllImport("pfxlibn.dll")]
public static extern Int32 PclOpen(Int32 iHandle, String sName, Int32 iIndex);
[DllImport("pfxlibn.dll")]
public static extern Int32 PclClear(Int32 iHandle);
[DllImport("pfxlibn.dll")]
public static extern Int32 PclPutField(Int32 iHandle, Int32 iFieldNum, String sData);
[DllImport("pfxlibn.dll")]
public static extern Int32 PclFind(Int32 iHandle, Int32 iRelOp, Int32 iIndex);
[DllImport("pfxlibn.dll")]
public static extern Int32 PclGetFieldLen(Int32 iHandle, Int32 iField, out int length);
error = CLib.PclOpen(_handle, path, 0);
error = CLib.PclClear(_handle);
error = CLib.PclPutField(_handle, 0, "10"); //searching for recnum 10
error = CLib.PclFind(_handle, 1, 0); // 0 = recnum index
error = CLib.PclGetFieldLen(_handle, iField, out flenght);
StringBuilder data = new StringBuilder(flenght);
CLib.PclGetField(_handle, iField, data);
if (error == 0)
{
return data.ToString();
}
我知道上面有廢話錯誤捕獲,我將很快修復。 我只想回復並感謝大衛的幫助。 另外,如果其他任何人都無法使用pfxlibn.dll導出數據,他們可能會發現這很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.