简体   繁体   English

如何导入Powerflex函数以在C#中使用?

[英]How should I import Powerflex functions for use in C#?

I have been given the task of using pfxlibn.dll in Visual Studio 2010 C#, which from what I can make out is a Powerflex library. 我已获得在Visual Studio 2010 C#中使用pfxlibn.dll的任务, 所知 ,这是一个Powerflex库。 http://pfxcorp.com/clib-fns.htm http://pfxcorp.com/clib-fns.htm

I have tried a few variations of importing the PclGetField function, for example: 我尝试了一些导入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);

So far, none of the above have worked. 到目前为止,以上方法均无效。 Or variations of the above. 还是以上的变化。

Within Delphi the code looks like 在Delphi中,代码看起来像

pTmpBuf           : PChar;

iLastErr := PclGetField(fHandle,iField,pTmpBuf);

The documentation you link to is very sparse so to answer the question we need to guess somewhat. 您链接到的文档非常稀少,因此要回答我们需要猜测的问题。

The functions you need are these: 您需要的功能如下:

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.

In order to know for sure how to handle this we need more information: 为了确定如何处理此问题,我们需要更多信息:

  • What does the PCLENTRY macro evaluate to? PCLENTRY宏会评估什么? I'm going to assume __stdcall , but you will need to check your header file. 我将假设__stdcall ,但是您将需要检查头文件。
  • What is aResult ? 什么是aResult I'm going to assume int but you will need to check your header file. 我将假定为int但您需要检查头文件。
  • What is aDHandle ? 什么是aDHandle I'm going to assume void* or INT_PTR but you will need to check your header file. 我将假定为void*INT_PTR但您需要检查头文件。
  • What is anInt ? 什么是anInt I'm going to assume int but you will need to check your header file. 我将假定为int但您需要检查头文件。
  • What character set is used? 使用什么字符集? ANSI or Unicode? ANSI还是Unicode? I'm going to assume ANSI. 我将假设使用ANSI。

You are going to need to call PclGetFieldLen to find out how big a buffer you need. 您将需要调用PclGetFieldLen来找出所需的缓冲区大小。 And then you will allocate that buffer and call PclGetField to populate it. 然后,您将分配该缓冲区并调用PclGetField进行填充。 You will need to find out whether or not the value returned by PclGetFieldLen includes the zero-terminator or not. 您将需要找出PclGetFieldLen返回的值是否包含零终止符。 I'm assuming that it does not. 我假设事实并非如此。

With these assumptions you would write the pinvoke like this: 基于这些假设,您可以这样编写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);

You can then call the functions like this: 然后,您可以像下面这样调用函数:

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();

Since we don't know all the details, there are a bunch of unknowns in this answer. 由于我们不了解所有详细信息,因此此答案中有一堆未知数。 You have the header file, and you can contact the library vendor to resolve the unknowns. 您具有头文件,并且可以与图书馆供应商联系以解决未知问题。 But hopefully the outline above tells you what questions need to be answered. 但希望上面的概述告诉您需要回答什么问题。

Thanks David Heffernan, However, i did not give enough information or research the delphi code enough. 感谢David Heffernan,但是,我没有提供足够的信息或没有足够研究delphi代码。

The main items that were missing was PclClear, PclPutField and PclFind. 缺少的主要项目是PclClear,PclPutField和PclFind。 I have since found the following works: 此后,我发现了以下作品:

    [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();  
        }

I know the above has crap error catching which i will be fixing up shortly. 我知道上面有废话错误捕获,我将很快修复。 I just want to reply and thank David for his help. 我只想回复并感谢大卫的帮助。 Plus if anyone else is stuck with export data using pfxlibn.dll they might find this helpful. 另外,如果其他任何人都无法使用pfxlibn.dll导出数据,他们可能会发现这很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM