在.NET中使用C ++应用程序 [英] Using C++ app in .NET

查看:115
本文介绍了在.NET中使用C ++应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉在经典C ++中使用PInvoke,昨天我问了这个问题:使用.NET中的Windows API功能

I am new to using PInvoke with Classic C++ and I asked this question yesterday: Using Windows API functions in .NET

我现在创建了一个非常简单的C ++程序,如下所示:

I have now created a very simple C++ program as follows:

#include <iostream>
#include <stdio.h>
#include <string>
extern "C" __declspec(dllexport) int hello()
{
  //printf ("Hello World!\n");
  return 1;
} 

然后我使用以下命令编译DLL:g ++ -c mydll.cpp 然后使用以下命令创建共享库:g ++ -shared -o mydll.dll mydll.o,然后将mydll.dll复制到C:\ Windows \ syswow64.

I then compiled the DLL using the following command: g++ -c mydll.cpp Then created the shared library using the following command: g++ -shared -o mydll.dll mydll.o, then copied mydll.dll to C:\Windows\syswow64.

然后我创建了一个新的VB.NET项目并创建了以下代码:

I then created a new VB.NET project and created the following code:

    Imports System.Runtime.InteropServices
Public Class TestPlatformInvoke
    <DllImport("mydll.dll", CallingConvention:=CallingConvention.Cdecl)> _
    Public Shared Function hello() As Integer
    End Function
    Public Sub New()
        Try
            Dim test As Integer = hello() 'Line 6 ex As Exception
            'I don't swallow exceptions
            MsgBox("test")
        Catch ex As Exception

        End Try
    End Sub
End Class

应用程序在调用hello()之后退出.它不会引发异常.

The application exits after calling hello(). It does not throw an exception.

我正在设法掌握它的工作原理.我对c ++没有任何商业经验;只有大学的学术经验.

I am trying to get to grips with how this works. I don't have any commercial experience with c++; only academic experience at university.

这是mydll.dll的Dependency Walker的图像.

Here is an image of Dependancy Walker for mydll.dll.

推荐答案

Declare是VB6的旧版.您应该使用p/invoke和DllImport属性.

Declare is VB6 legacy. You should use p/invoke and the DllImport attribute.

<DllImport("mydll.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function hello() As Integer
End Function

有很多方法可能会失败.可能由于32/64位不匹配,导致DLL无法加载.此外,您的呼叫约定不匹配.您的DLL将使用cdecl,但您的VB代码使用stdcall.上面的pinvoke可以解决此问题.

There are lots of ways that this could fail. Perhaps the DLL is not loading because there's a 32/64 bit mismatch. Also, your calling conventions do not match. Your DLL will be using cdecl but your VB code uses stdcall. The pinvoke above fixes that.

最严重的是,您的函数似乎未从DLL中导出.这肯定会使它失败.使用Dependency Walker确定您的函数是否已导出.我对g ++不太熟悉,因此您必须弄清楚如何使用GNU工具链导出函数.但是请注意名称修饰和名称修饰.您可能需要注意如何导出函数,以便使用所需的名称导出该函数.

Most seriously your function does not appear to have been exported from the DLL. That's going to make it fail for sure. Use Dependency Walker to determined whether or not your function has been exported. I'm not so familiar with g++ so you'll have to work out how to export your function using the GNU toolchain. But watch out for name decoration and name mangling. You may need to take care with how you export your function so that it is exported with the desired name.

这篇关于在.NET中使用C ++应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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