Hello,
I am calling a c++ dll in vb...
Everything is working fine..
Just i am unable to write the vb code in order to perform the require action...

Code for calling the c++ dll in vb

Private Declare Function do_encrypt Lib "AES.dll" (ByVal text As String, ByVal keyz As String) As String

So i have 3 text boxes and a command button on the form
text1.text = takes text
text2.text = takes keyz
and when i click on Command1_Click() the result must be displayed in text3
I have tried

Private Sub Command1_Click()
Text3.text = Str(do_encrypt, Val(Text1.text), Val(Text2.text))
End Sub

Its not working can anyone help how we can solve the problem

Dani AI

Generated

Quick summary of the typical problems and a small checklist—this thread already hit the main points: C++ ABI/strings, calling convention, and VB type mismatches. was right that std::string won't cross the boundary; 's suggestion to register only applies to COM/ActiveX DLLs (not plain Win32 exports); and the display issue was just VB showing a small double in scientific notation (that can be formatted on the VB side).

Checklist (what to verify, in order)

  1. Export with C linkage and the stdcall convention (VB expects stdcall). Avoid C++ name-mangling.
  2. Use C-compatible parameter and return types across the boundary (no std::string). For strings, pass C-style const char* (VB: ByVal s As String) or use COM/BSTR if you need Unicode/COM semantics.
  3. Match numeric sizes: VB6 Integer is 16-bit; use Long in VB to match a 32-bit C int. Map double <-> VB Double.
  4. Do not run regsvr32 on a plain exported DLL—registering is only for COM/ActiveX.

Minimal example of the contract (illustrative)

extern "C" __declspec(dllexport) double __stdcall ComputeTime(const char* infile, const char* key);

and the VB declare should match the name, parameter order, calling convention and return type.

Debugging tips

  • Use tools (dumpbin /exports or Dependency Walker) to confirm the exact exported name and calling convention.
  • Avoid relying on cout/printf when your code is called from a GUI — output may not appear. Use OutputDebugString, log to a file, or test with a console stub.
  • If returning floating values, format them in VB to avoid exponential notation (this is what fixed the display in the thread).

If you still get crashes, post the exact C++ export signature, the VB Declare line, and the tool output showing exports so the mismatch can be pointed out precisely.

Recommended Answers

All 16 Replies

must be some syntax errors. rectify like this :-

Text3.text=do_encrypt(Text1.text,Text2.text)

regards
Shouvik

Hello I have tired to change the code in the comand1_click() to

Text3.text=do_encrypt(Text1.text,Text2.text)

but still getting errors...
I am posting the codes in zip file...
Can anyone help how we can solve the problem....

Hello,
I am calling a c++ dll in vb..
The c++ code function is returning a value as a double...

double _stdcall aesen(string path1, string path2, string path3)
{double time;
     .
     .
     .
    return time;
}

Now in the VB code:-I have called the example2.dll

Private Declare Function aesen Lib "example2.dll" (ByVal path1 As String, ByVal path2 As String, ByVal path3 As String) As String

Now I want to get the time in the textbox5
I have wriiten the code:-

Private Sub Command5_Click()
Text5.text = aesen(Text1.text, Text2.text, Text3.text)
End Sub

Its not responding and i have to end the program...
Can anyone help...

did you try to register the dll with your operating system?

if not then try using this command :-

regsvr32 <your dll file name>.Dll

then create a reference to the file from Project->References

No i have not registered the dll with the OS...
I will try it...
Btw can we have cout/printf statements inside the dll and will it work correctly?...

Hello,
I am posting a new code in zip format...
Can anyone help just to be able to call the function aesen from the c++ dll and display the time in one of the textbox...
I have tried a function that converts low case letters to uppercase its working...
When running the vb forms text2.text takes p.txt and text3.text = encrypt.txt and text4.text= text.txt and the button encrypt must display the time for the keyshe_time...
The time that is returned in the c++ dll
Can anyone help me to solve this problem...

Hello,
I have a function written in c++ to get the size of a file...
I have tested it by calling it in the main...
Thats working fine...Now i have created a dll of the code and call it form the vb
Now if i call it from the vb i am getting an error message c++ assertion debug,abort,retry
The c++ code...

#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <windows.h>
#include <string.h>
#include <memory.h>
#include <process.h>
#include <tchar.h>
#include <dos.h>
#include <conio.h>
#include <fstream>
#include <string>
# include <bitset>
#include <cstring>
#include <algorithm>
using namespace std;

#include <time.h>

int _stdcall ss(string de)
{
    int size;
    ifstream myfile456 (de.c_str(), ios::binary);
	myfile456.seekg (0, ios::end);
	size = myfile456.tellg();
	cout << "File size is: " << size << " bytes.\n";
	return (size);
}

int main()
{
	ss("c:/p.txt");
	return 0;
}

Thats working fine...
In the VB code i am calling it by...using this code that is the dll

Private Declare Function ss Lib "testing.dll" (ByVal de As String) As Integer

Now in the vb code i have a browse button to select the file p.txt and i get the path in a textbox2..That is i open the file p.txt and gets its path in the textbox2..
Now when i click the button 1 its not working...and i have to display the size of the file in the textbox3

Private Sub Command1_Click()
Text3.Text = ss(Text2.Text)
End Sub

Can anyone help to solve the problem..

I hope you realize that std::string in c++ programs is not the same as a String in VB. The function exported in the dll needs to be written in such a way that it can be called from languages other than c++, such as this: int _stdcall ss(const char* de) or this int _stdcall ss(VARIANT* de)

Thanks Ancient dragon that really works...
Thanks again...

It is always recommended to register the component with windows and then use it in VB 6.0.
Add the same from Project---->References

and then use it.

Hello,
I have a c++ code(function) which returns the time in double...
Now i am calling this function in VB and i am displaying the time in a textbox...
I am getting a different value...

double encryptTime, Add, sub, sft, mix;
int _stdcall encrypt()
{// calculate add sub sft mix
encryptTime = Add + sub + sft + mix;
printf("Encrytion Time : ");
printf("%lf ", encryptTime);
printf("seconds.");
printf("\n\n");
return 0;
}

double _stdcall encrypt_Encryption_Time()
{
	return (encryptTime);
}

The time i am getting is:-

0.000142 seconds

The c++ dll is called in VB:-

Private Declare Function encrypt Lib "example2.dll" () As Integer
Private Declare Function encrypt_Encryption_Time Lib "example2.dll" () As Double

The command button to click and display the value in a textbox

Private Sub Command6_Click()
Call Encrypt_Text()
Text11.text = encrypt_Encryption_Time()
End Sub

I am getting the time on the textbox11 as

1.42476208568407E-04

Can anyone help to solve the problem..

Hello,
I think that the problem is with the return type of the function that is double...
Is double in c++ and VB the same?
That is if i am returning a double in a function in c++ and display the returned value in a textbox in VB...will it work correctly?...

It is always recommended to register the component with windows and then use it in VB 6.0.
Add the same from Project---->References

and then use it.

Most dlls can not be registered.

Hello,
In fact i am able to call a function which returns an int from a c++ code and display the value in the vb textbox... thats works fine...
Now i am calling the c++ function which returns a double and i am displaying the values in the vb textbox it is not displaying the correct value...

Original value: 0.000142
Value on the vb textbox: 1.42476208568407E-04

How can we solve the problem..

>> it is not displaying the correct value
Yes it is -- it is displaying it in scientific notation. You can do the same thing in C by using "%e" instead of "%f", for example:

int main()
{
    double x = 0.000142;
    printf("%f\n", x);
    printf("%e\n", x);
    return 0;
}

0.000142
1.420000e-004
Press any key to continue . . .

Hello Ancient Dragon,
Thnx again the tutorial that really help me out to solve the problem....
The tutorial link is as follows:
I have modified the code as follows so that VB displays the values on the textbox as the user wants to display it in a format specified by him...
The modified code is as follows:-

Text11.text = Format(encrypt_Encryption_Time(), "#.######")

The output on the textbox is as: 0.000142
Thanks Ancient Dragon for the help...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.