1 Attachment(s)
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:
Attachment 188552
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.
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.
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.
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.
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.
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.
Re: Using a dll made by c++ for VB
Re: Using a dll made by c++ for VB
Quote:
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.
Re: Using a dll made by c++ for VB
Yes I did, is that a problem?
Re: Using a dll made by c++ for VB
Quote:
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.
Quote:
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.
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?
Quote:
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.
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.
Re: Using a dll made by c++ for VB
Quote:
Originally Posted by
PhotonBytes
Can you show me where you said something twice to me? Many apologies.
I sure can.
Quote:
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.
Quote:
Originally Posted by
jmcilhinney
As I said previously, you will have to use pinvoke to call the unmanaged functions exported by your library.
Quote:
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.
Quote:
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?
Re: Using a dll made by c++ for VB
Quote:
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?
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?
Re: Using a dll made by c++ for VB
Quote:
Originally Posted by
PhotonBytes
Please confirm if I understand correctly
You do not.
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
Re: Using a dll made by c++ for VB
Quote:
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.
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.
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
1 Attachment(s)
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.
Attachment 188554
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.
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!
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!
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';
}