如何在C中使用DLL C ++(VB)? [英] How to use DLL C++ (VB) in C ?

查看:106
本文介绍了如何在C中使用DLL C ++(VB)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个例子中编写了这段代码。

我在Visual Basic C ++中编写了这段代码。现在我想用C调用它。

我希望得到20分C.

你能指导吗?

谢谢。

我用C ++创建了这个Dll:



我尝试了什么:



I wrote this code in an example.
I wrote this code in Visual Basic C++. Now I want to call it in C.
I want to get 20 in C.
Can you guide?
thanks.
I created this Dll in C++ :

What I have tried:

extern "C" __declspec(dllexport) int number(int a); 
int DllMain()
{
    a=20;        
    return (a);
}

推荐答案

如果你真的使用Visual Basic,那么根本不容易。

首先,Visual Basic对C ++一无所知 - 它是一种托管的.NET语言,这意味着你需要在机器上安装和加载适当的.NET框架才能使用它。



C不是托管的,它是原生的 - 它对.NET一无所知 - 所以它变得非常复杂:从C调用C# - 堆栈溢出 [ ^ ](C#是另一种.NET语言,它与VB完全相同)。



如果你想从C调用本机C ++函数,那就更容易了:混音C和C ++代码同一计划 [ ^ ]

只是不要再使用Visual Basic一词 - 它与C或C ++无关!
If you are genuinely using Visual Basic, then "not at all easily".
First off, Visual Basic has nothing at all to d with C++ - it's a managed .NET language, which means you need the appropriate .NET framework installed and loaded on the machine in order to use it.

C is not managed, it's native - it knows nothing at all about .NET - so it gets very complicated: Calling C# from C - Stack Overflow[^] (C# is another .NET language, and it's exactly the same procedure for VB).

If you want to call native C++ functions from C, that's easier: Mixing C and C++ Code in the Same Program[^]
Just don;t use the term "Visual Basic" again - it's nothing to do with C or C++!


你不能除非您提供适当的界面,否则请使用任何语言。首次加载库时,Windows会调用DllMain,它不是用户函数。请参阅创建和使用动态链接库( C ++)| Microsoft Docs [ ^ ]。



此外没有 Visual Basic C ++ 这样的东西。
You cannot use it in any language unless you provide a proper interface. DllMain is called by Windows when the library is first loaded, it is not a user function. See Creating and Using a Dynamic Link Library (C++) | Microsoft Docs[^].

Also there is no such thing as Visual Basic C++.


你编写的代码通过声明数字函数有一个主要缺陷,但是实现了main。在标题和c文件中的实现中写下声明。



必须具有与声明的相同的签名。所以:

The code that you wrote has a main flaw by declaring the number function, but implementing the the main. Write the declaration in the header and the implementation in the c-file.

It MUST have the same signature as declared. So:
int number(int a)
{
    a=20;        
    return (a);
}

通过 GetProcAdress 非常简单,但请注意签名和每个错误。

Calling dlls via GetProcAdress is very easy, But take care on the signature and every error.


这篇关于如何在C中使用DLL C ++(VB)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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