在C#中使用非托管代码 [英] using un-managed code in C#

查看:130
本文介绍了在C#中使用非托管代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与C#中一样,使用以下代码:
裁剪简单

Header.h

As in C # use the following code:
Cutting simple

Header.h

extern "C" class __declspec (dllexport)  MyClass
public:
{bool Open(int port, int baud);}



Prog.cpp



Prog.cpp

extern "C" __declspec (dllexport) bool MyClass::Open(int port, int baud)
{
    char COM_string[20];
    sprintf(COM_string,"\\\\.\\COM%d", port);

    m_hFile = CreateFile(COM_string, GENERIC_READ|GENERIC_WRITE, 0, NULL,
       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);


    if(m_hFile == INVALID_HANDLE_VALUE)
    {
        return false;
    }
}



这被编译到MyLybrary.dll库中.
在C#中,我使用以下属性:



This is compiled into the library MyLybrary.dll
In C # I use the attribute:

[DllImport("MyLybrary", EntryPoint = "Open")]
            public static extern bool Open(int port, int baud);



但是调用Open()以获得消息:找不到Open的入口点.

问题是使用Class MyClass吗?



But the call Open() to get the message: Can not find an entry point Open.

The problem is the use of Class MyClass?

推荐答案

打开似乎不是入口点,我认为应该运行得很好,而无需声明入口点..
Open should not seem an entry point, i think should go well so as is without declaring entry point ..


我认为问题在于您正在调用的函数在类中.
I think the problem is that the function you are calling is in a class.


这篇关于在C#中使用非托管代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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