如何在C中使用DLL C ++(VB)?

编程入门 行业动态 更新时间:2024-10-28 00:17:09
本文介绍了如何在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)?

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

发布评论

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

>www.elefans.com

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