Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
I created this Dll in C++ :

What I have tried:

extern "C" __declspec(dllexport) int number(int a); 
int DllMain()
    return (a);
Updated 16-Dec-18 6:43am

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++!
Share this answer
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++.
Share this answer
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)
    return (a);
Calling dlls via GetProcAdress is very easy, But take care on the signature and every error.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900