我应该如何导入Powerflex函数以用于C#?

use*_*00a -1 c# delphi pinvoke

我被赋予了在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);
Run Code Online (Sandbox Code Playgroud)

到目前为止,上述都没有奏效.或以上的变化.

在Delphi中,代码看起来像

pTmpBuf           : PChar;

iLastErr := PclGetField(fHandle,iField,pTmpBuf);
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 8

您链接到的文档非常稀疏,所以要回答我们需要猜测的问题.

您需要的功能是:

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.
Run Code Online (Sandbox Code Playgroud)

为了确定如何处理这个问题,我们需要更多信息:

  • 什么是PCLENTRY宏观评估来?我将假设__stdcall,但你需要检查你的头文件.
  • 什么是aResult?我将假设int你需要检查你的头文件.
  • 什么是aDHandle?我要假设void*,INT_PTR但你需要检查你的头文件.
  • 什么是anInt?我将假设int你需要检查你的头文件.
  • 使用什么字符集?ANSI还是Unicode?我将假设ANSI.

您需要打电话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);
Run Code Online (Sandbox Code Playgroud)

然后你可以调用这样的函数:

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();
Run Code Online (Sandbox Code Playgroud)

由于我们不了解所有细节,因此在这个答案中有许多未知数.您有头文件,您可以联系库供应商以解决未知问题.但希望上面的大纲告诉你需要回答哪些问题.