在.NET中使用C ++应用程序

编程入门 行业动态 更新时间:2024-10-17 18:20:43
本文介绍了在.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 ++应用程序

本文发布于:2023-11-07 05:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!