通过错误的名称进行P/调用功能 [英] P/Invoking function via a mangled name

查看:76
本文介绍了通过错误的名称进行P/调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Windows ce 6.0环境中的.net cf 3.5中的非托管c ++ dll中调用函数.

I'm trying to call an function in an unmanaged c++ dll in .net cf 3.5 in a windows ce 6.0 environment.

该结构定义为:

typedef struct TagOperatorInfo
{
    DWORD dwMode;       
    DWORD dwFormat;     //Operator name format
    DWORD dwAct;        //Network type(Available in 3G module£ºGSM or 3G),
    TCHAR szOper[32];   
}OperatorInfo,*LPOperatorInfo;

函数调用为:

BOOL GetCurOperatorInfo(LPOperatorInfo pinfo);

我在.net中定义了TagOperatorInfo,如下所示:

I defined in .net the TagOperatorInfo as follows:

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public struct TagOperatorInfo
    {

        /// DWORD->unsigned int
        public uint dwMode;

        /// DWORD->unsigned int
        public uint dwFormat;

        /// DWORD->unsigned int
        public uint dwAct;

        /// TCHAR[32]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 32)]
        public string szOper;
    }

,在看了一些文章和msdn文档之后,我将本机函数称为:

and after seeing some articles and msdn documentation i call the native function as:

[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "#30", CallingConvention = CallingConvention.Winapi)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)] ref NativeHelper.TagOperatorInfo operatorInfo);

注意:因为c ++混淆了名称,所以我用序号定义了一个入口点来调用该函数.

Note: I call the function with an entry point defined with the ordinal because the c++ mangled names.

我的问题是,我总是会抛出notSupportedException异常.我不明白,因为ref参数应该给它指向结构的指针.

The problem that i have is that i always get the notSupportedException throw. I don't understand because the ref parameter should give it the Pointer to the struct.

调用它的.net函数是:

My .net function that calls it is:

/// <summary>
    /// Gets the current operator information.
    /// </summary>
    /// <param name="operatorInfo">The operator info.</param>
    /// <returns></returns>
    public static bool GetOperatorInformation(out NativeHelper.TagOperatorInfo operatorInfo)
    {
        operatorInfo = new NativeHelper.TagOperatorInfo();

        if (NativeImports.GetCurOperatorInfo(ref operatorInfo) == true)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

要实现这一目标,我缺少什么.

What i'm missing for this to work.

更新

新的 .Net Compact Framework 方法调用

[System.Runtime.InteropServices.DllImportAttribute(gsmaAdapterDLLName, EntryPoint = "?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z", CallingConvention = CallingConvention.Winapi)]
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    internal static extern bool GetCurOperatorInfo([MarshalAs(UnmanagedType.LPStruct)]ref NativeHelper.TagOperatorInfo operatorInfo);

推荐答案

您只能调用已导出为C样式函数的函数.据我所知,您无法通过P/Invoke调用直接的C ++函数,例如:

You can only call functions that have been exported as C style functions. As far as I am aware you cannot call straight C++ functions via P/Invoke, e.g.:

更新

实际上,通过P/Invoke调用函数时,确实可以使用乱码名称.这是我过去无法完成的工作,因此我已纠正.使用名称而不是普通名称也应该更具弹性:

Actually, it does appear you can use mangled names when calling a function via P/Invoke. This is something I could never get working in the past, so I stand corrected. Using names rather than ordinals should be more resilient too:

参考:找不到入口点异常

所以像这样:

[DllImport(gsmaAdapterDLLName,
    EntryPoint="?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z", 
    ExactSpelling = true,
    CallingConvention = CallingConvention.Cdecl)]
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info);

对于.Net CF:

DllImport(gsmaAdapterDLLName,
    EntryPoint="?GetCurOperatorInfo@CGSMAdapter@@YAHPAUTagOperatorInfo@@@Z", 
    CallingConvention = CallingConvention.WinApi)]
public static extern bool GetCurOperatorInfo(out TagOperatorInfo info);

这篇关于通过错误的名称进行P/调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆