|
-
Aug 22nd, 2023, 02:21 AM
#1
Thread Starter
Junior Member
Using a dll made by c++ for VB
I'm using VS2019 for both c++ and VB. However even after upgrading all my projects to 2019 level from 2010 and 6.0 I am still having trouble with this issue. All I want to do is write a simple dll in c++ and then get VB to be able to use it. But when I try to reference the dll from VB I get the following message:
-
Aug 22nd, 2023, 03:17 AM
#2
Re: Using a dll made by c++ for VB
Perhaps you could copy/paste the error message into a thread so that we might have some chance of actually being able to read it.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Aug 22nd, 2023, 03:32 AM
#3
Re: Using a dll made by c++ for VB
You can use the Windows Clipboard to copy the text from a dialogue like that so there's no excuse for posting a picture instead of text. If you were going to post a picture, there's no excuse for posting your entire screen instead of just the part we need when Windows has a Snipping Tool built right in. Put some effort into making your question easy to answer, which includes easy to read, and we won't have to waste time going back and forth like this, meaning that you'll get the help you want sooner.
Having said that, is your C++ DLL a .NET assembly, i.e. built from a C++/CLI project, or a COM library? If not then you can't reference it. If it exports functions directly then you need to use Platform Invoke (pinvoke), just as you do for Windows API functions.
-
Aug 22nd, 2023, 04:18 AM
#4
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
A reference to : 'c:\...\RungeKutta.dll' could not be added. Please make sure that the file is accessible and that it is a valid assembly or COM component.
-
Aug 22nd, 2023, 04:47 AM
#5
Re: Using a dll made by c++ for VB
Please pay attention. You posted the same thing to this thread four times and then created a new thread for no good reason. Because you are a new member, your posts require approval by a moderator, which is why they didn't show up immediately. You would have been told this when you posted. This is an inauspicious start to your time here. Hopefully you can improve the quality of your posts in future.
-
Aug 22nd, 2023, 04:52 AM
#6
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
I think you need a form that says that your message has been submitted, instead as soon as i click submit it brings me straight back here with no receipt.
-
Aug 22nd, 2023, 04:53 AM
#7
Re: Using a dll made by c++ for VB
As for the issue, the error message pretty much confirms what I suspected. You can only reference .NET assemblies and COM components in .NET projects, so your C++ DLL is almost certainly neither. As I said previously, you will have to use pinvoke to call the unmanaged functions exported by your library.
Where did you get this DLL? Is there any documentation with it that provides .NET examples? If not, you'll just have to research pinvoke to learn the principles involved, then apply those principles to your specific scenario. Basically, you declare a .NET method with the appropriate attributes that matches the signature of the unmanaged function you want to call. You then call your method and .NET will invoke the external function. You'll then need to deploy your unmanaged library with your .NET app. You might start here.
-
Aug 22nd, 2023, 05:11 AM
#8
Re: Using a dll made by c++ for VB
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Aug 22nd, 2023, 05:14 AM
#9
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
 Originally Posted by jmcilhinney
As for the issue, the error message pretty much confirms what I suspected. You can only reference .NET assemblies and COM components in .NET projects, so your C++ DLL is almost certainly neither. As I said previously, you will have to use pinvoke to call the unmanaged functions exported by your library.
Where did you get this DLL? Is there any documentation with it that provides .NET examples? If not, you'll just have to research pinvoke to learn the principles involved, then apply those principles to your specific scenario. Basically, you declare a .NET method with the appropriate attributes that matches the signature of the unmanaged function you want to call. You then call your method and .NET will invoke the external function. You'll then need to deploy your unmanaged library with your .NET app. You might start here.
Hello, I created the dll my self, it is a physics simulator. All it does is it simulats particle physics using discrete mathematics of the Runge Kutta. I used a dll template from chatbot. And then copy pasted my code into new header files one by one to ensure there are no complatibility issues from transporting code from an old version of c++ VS6.0.
How do I make my dll work with VB? Can you provide a template dll for VSC++ 2019 that already has working system and properties that is compatible with VB 2019? Then I can just do the same as before and copy paste my source code into it, object by object.
-
Aug 22nd, 2023, 05:35 AM
#10
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Yes I did, is that a problem?
-
Aug 22nd, 2023, 05:55 AM
#11
Re: Using a dll made by c++ for VB
 Originally Posted by PhotonBytes
How do I make my dll work with VB?
I've already told you that twice. If you make the effort to learn then we can help you with specific issues, but we can't tell you how to use a library that we know nothing about and it's not for us to basically do it for you even if we had seen it.
 Originally Posted by PhotonBytes
Can you provide a template dll for VSC++ 2019...
This is a VB forum, so it's not for us to answer C++ questions or teach you how to use C++/CLI. If you want to create a .NET assembly using C++ so that you can reference it directly, you'll need to learn how to use C++/CLI for yourself.
-
Aug 22nd, 2023, 06:19 AM
#12
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Sorry I do not recall you saying it twice, I can see you have said something once. Can you show me where you said something twice to me? Many apologies. I am unfamiliar with your customs here.
If there is nothing for me to do on the C++ side what can I do on the VB side to make my c++ dll work?
 Originally Posted by jmcilhinney
I've already told you that twice. If you make the effort to learn then we can help you with specific issues, but we can't tell you how to use a library that we know nothing about and it's not for us to basically do it for you even if we had seen it.
This is a VB forum, so it's not for us to answer C++ questions or teach you how to use C++/CLI. If you want to create a .NET assembly using C++ so that you can reference it directly, you'll need to learn how to use C++/CLI for yourself.
-
Aug 22nd, 2023, 06:58 AM
#13
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
May I summarize your answers as follows?
built from a
1. C++/CLI project, or a
2. COM library? If not then you can't reference it. If it exports functions directly then you need to use
3. Platform Invoke (pinvoke), just as you do for Windows API functions.
Sounds like option 3 is the easiest to do. This I gather is something I do in c++ coding yes? I only ask because you brought it up.
-
Aug 22nd, 2023, 07:49 AM
#14
Re: Using a dll made by c++ for VB
 Originally Posted by PhotonBytes
Can you show me where you said something twice to me? Many apologies.
I sure can.
 Originally Posted by jmcilhinney
If it exports functions directly then you need to use Platform Invoke (pinvoke), just as you do for Windows API functions.
 Originally Posted by jmcilhinney
As I said previously, you will have to use pinvoke to call the unmanaged functions exported by your library.
 Originally Posted by PhotonBytes
If there is nothing for me to do on the C++ side what can I do on the VB side to make my c++ dll work?
Exactly what I already told you to do.
 Originally Posted by jmcilhinney
Basically, you declare a .NET method with the appropriate attributes that matches the signature of the unmanaged function you want to call. You then call your method and .NET will invoke the external function. You'll then need to deploy your unmanaged library with your .NET app. You might start here.
Did you follow the link that I went to the trouble of providing as a starting point for you, which provides an example of the sort of thing I described?
-
Aug 22nd, 2023, 08:35 AM
#15
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
 Originally Posted by jmcilhinney
I sure can.
Exactly what I already told you to do.
Did you follow the link that I went to the trouble of providing as a starting point for you, which provides an example of the sort of thing I described?
Sorry, I didn't know I offended you with this. I feel like I am ment to be apologizing at this point, I didn't realize I have to be so careful in this forum. I will do my best to meet your expectations. The three statements look different from me, more like 3 different statements than saying the same things twice. I don't see you supplying a link for me to look, did it look like a blue hyperlink?
-
Aug 23rd, 2023, 03:28 AM
#16
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Please confirm if I understand correctly, all I have to do is wrap my dll code inside:
value class Win32 {}
and it will work yes?
-
Aug 23rd, 2023, 03:35 AM
#17
Re: Using a dll made by c++ for VB
 Originally Posted by PhotonBytes
Please confirm if I understand correctly
You do not.
-
Aug 23rd, 2023, 09:37 AM
#18
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Even if I did the solutions provided above the problem remains: The VB 2019 IDE rejects the dll as a reference, are there any solutions to this problem?
The error message again is: A reference to : 'c:\...\RungeKutta.dll' could not be added. Please make sure that the file is accessible and that it is a valid assembly or COM component.
Below is a link to my dll which works with other c++ projects but fails with VB 2019
https://drive.google.com/drive/folde...lm?usp=sharing
-
Aug 23rd, 2023, 10:51 AM
#19
Re: Using a dll made by c++ for VB
 Originally Posted by PhotonBytes
The VB 2019 IDE rejects the dll as a reference, are there any solutions to this problem?
I've told you why that is and I've told you what to do about it. It seems like it's just too much trouble for you to do any research on the specific subject I've told and are just waiting for someone to give you something to copy and paste. Good luck with that because I'm out of here. Can only spend so much time on people who won't listen.
-
Aug 23rd, 2023, 10:58 AM
#20
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Just tell me this, if I did what you say, would it remove the error message when I attempt to reference the dll from VB?
" Platform Invoke (pinvoke), just as you do for Windows API functions." - I do this from the VB or C++ side? If from VB side will it force VB to accept the dll without error message when I try to reference it from the menu? You mean some lines of code will force it to accept it?
Or are you saying I SHOULD NOT TRY AND REFERENCE THE DLL from the menu of the VB IDE?
Apologies for the seeming lack effort, im typing from my hospital bed. Im sick.
Last edited by PhotonBytes; Aug 23rd, 2023 at 02:14 PM.
-
Aug 23rd, 2023, 02:29 PM
#21
Re: Using a dll made by c++ for VB
You CANNOT reference the DLL ... it is neither a .NET nor a COM DLL ... there is nothing to reference. So, no, you cannot right-click -> add reference... You have to import the DLL, or rather import the method you plan to use with it.
See this article - https://stackoverflow.com/questions/...voke-in-vb-net
And this - https://learn.microsoft.com/en-us/do...nterop/pinvoke (note, this is skewed to C#, but the principle is the same, add a DLLImport with the proper signature)
and this - https://stackoverflow.com/questions/...port-in-vb-net
and this - https://learn.microsoft.com/en-us/do...e?view=net-7.0
and this - https://learn.microsoft.com/en-us/do...g-windows-apis
should be enough material to go through to get started.
-tg
-
Aug 24th, 2023, 05:40 AM
#22
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
I got some mixed progress going by your advice:
In my VB code:
Public Class MyProject
' Declare the P/Invoke signature for the DoubleValue function
<DllImport("RungeKutta.dll", CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function RK_Animate(ByVal value As Double)
End Function
<DllImport("RungeKutta.dll", CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function RK_Init() As Integer
End Function
Above I asked ChatGBT to help me with this code above of using p/invoke.
VB:
Private Sub MyProject_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RK_Init()
RK_Animate(1)
On the C++ Side I have
in my RungeKutta.H
RUNGEKUTTA_API void RK_Animate(double TimeInterval);
extern RUNGEKUTTA_API void RK_Animate(double);
RUNGEKUTTA_API int RK_Init(void);
extern RUNGEKUTTA_API int RK_Init();
RungeKutta.CPP
// An exported function.
RUNGEKUTTA_API void RK_Animate(double TimeInterval)
{
/*
printf("RK_Animate version VS2010Express version TimeInterval = %f",TimeInterval);
if(RK_Universe->Size>0)
{
RK_Universe->Animate(TimeInterval);
}
else
{
printf("\nRK_Animate: Size = 0");
}
*/
}
RUNGEKUTTA_API int RK_Init()
{
static bool alreadycalled = false;
if (alreadycalled == false)
{
printf("\n==================== Hello THIS IS THE INIT FOR RK");
RK_Universe = new ObjectList;
RK_Universe->AddSystem();
printf("\nRK_Universe->Size = %d", RK_Universe->Size);
printf("\nRK_Universe->SizeGravitational = %d", RK_Universe->SizeGravitational);
printf("\nRK_Universe->SizeNonGravitational = %d", RK_Universe->SizeNonGravitational);
printf("\n==================== Hello THIS IS THE END OF INIT FOR RK");
alreadycalled = true;
return 11;
}
else
{
printf("\n==================== Hello THIS IS THE INIT FOR RK BUT ITS ALREADY CALLED SO NOTHING HAPPENED THIS TIME ROUND");
return 101;
}
}
For debugging purposes I have coment out code in the first function but I am still getting the following errors:
VB SIDE:
For RK_Init()
System.EntryPointNotFoundException
HResult=0x80131523
Message=Unable to find an entry point named 'RK_Init' in DLL 'RungeKutta.dll'.
Source=Part2
StackTrace:
at Part2.MyProject.RK_Init()
at Part2.MyProject.MyProject_Load(Object sender, EventArgs e) in C:\Users\david\OneDrive\WORKSPACE\VB\VB2019\REVAMP 6\MyProject.vb:line 280
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
For RK_Animate(1)
System.Runtime.InteropServices.MarshalDirectiveException
HResult=0x80131535
Message=PInvoke restriction: cannot return variants.
Source=Part2
StackTrace:
at Part2.MyProject.RK_Animate(Double value)
at Part2.MyProject.MyProject_Load(Object sender, EventArgs e) in C:\Users\david\OneDrive\WORKSPACE\VB\VB2019\REVAMP 6\MyProject.vb:line 281
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I don't know what to make of it. ChatBotGBT is no help here because it says to check the arguments which in this case are either void or integer.

HIGH REZ IMAGES:
https://drive.google.com/file/d/1ea7...ew?usp=sharing
https://drive.google.com/file/d/1Ca5...ew?usp=sharing
https://drive.google.com/file/d/159C...ew?usp=sharing
https://drive.google.com/file/d/1vBk...ew?usp=sharing
https://drive.google.com/file/d/1lwp...ew?usp=sharing
Many thanks for getting me this far.
On a side note Im hiring experts to help me with problems like this as a freelance consultant. Do let me know if you want paid work.
Last edited by PhotonBytes; Aug 24th, 2023 at 06:44 AM.
-
Aug 24th, 2023, 10:38 AM
#23
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
May I suggest to help me, if any of you are able to write a small VSC++ project with pinvoke that has a function that simply doubles a number? And have it work in VB?
If you can do that can you send me the source code so I can use it as a template?
So far suggestions have failed because they created new problems.
Im watching:
https://www.youtube.com/watch?v=hYZjhXkxT74
Now. Trying to see if this solution works. With this solution it says I need a def file. What a pain in the butt! There's no file to download from the youtube site. So will have to manually type everything!
Last edited by PhotonBytes; Aug 24th, 2023 at 11:12 AM.
-
Aug 24th, 2023, 12:19 PM
#24
Thread Starter
Junior Member
Re: Using a dll made by c++ for VB
Hi all after, including the .def file in the youtube tutorial i managed to double a number in VB using a c++ dll. Thanks so much for your patience!
-
Aug 25th, 2023, 04:53 AM
#25
Re: Using a dll made by c++ for VB
When you compile your c++ dll code, you should get generated .dll, .exp and .lib files. When using a .dll from c/c++ code you'd usually specify the .lib file to use that contains details of the .dll file(s) being used. I don't use VB so don't know how that works. If you really do need a .def file then see this thread re automatically creating one from the .dll file
https://forums.codeguru.com/showthre...-DLL-available
That link includes a C++ program to create a .def file from the output from dumpbin /exports
For minimal c++ code for an exported .dll function to double an int:
mydll.h
Code:
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef NUM_EXPORT
#define NUM_API extern "C" __declspec(dllexport)
#else
#define NUM_API extern "C" __declspec(dllimport)
#endif
NUM_API int intdouble(int i);
mydll.cpp
Code:
#define NUM_EXPORT
#include "mydll.h"
int intdouble(int i) {
return i + i;
}
and a simple C++ test program (sorry, I don't use VB so can't provide a VB usage example):
Code:
#include <iostream>
#include "mydll.h"
#pragma comment (lib, "mydll.lib")
int main() {
std::cout << "double 21 is " << intdouble(21) << '\n';
}
Last edited by 2kaud; Aug 25th, 2023 at 09:58 AM.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|