Now I'm trying to get a char string as the below c function.
This is a C function of DLL,
char _stdcall *fmt_hex(long long num, int nbits, char *d)
{
sprintf(d, "0x%0*llX", (nbits - 1) / 4 + 1, num);
return (d);
}
and the below is the Excel VBA function.
Option Explicit
Private Declare PtrSafe Function pll_dll Lib "F:\work\pll_dll\x64\Debug\pll_dll.dll" (ByVal num As LongLong, ByVal nbits As Integer, ByRef d As String)
Dim d As String
Sub useSquareInVBA()
Cells(4, 4).Text = pll_dll(3, 4, d)
End Sub
but I've got a program halt when I run this code. Can you give me what am I supposed to do to resolve this problem?
When I ran with this code "C"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double _stdcall pll_dll(double* datain0, double* datain1, double* dataout0, double* dataout1, char * str)
{
dataout0[0] = datain0[0] + 10;
dataout1[0] = datain1[0] + 10;
str ="c";
return 0;
}
"VBA"
Option Explicit
Private Declare PtrSafe Function pll_dll Lib "F:\work\pll_dll\x64\Debug\pll_dll.dll" _
(ByRef x_in As Double, ByRef y_in As Double, ByRef x_out As Double, ByRef y_out As Double, ByRef str As String) As Double
Dim Error As Integer
Dim d1 As Double
Dim d2 As Double
Dim d3 As Double
Dim d4 As Double
Dim sometext As String
Function pll_dll_excel(data1 As Double, data2 As Double, data3 As Double, data4 As Double, text As String) As Double
pll_dll_excel = pll_dll(data1, data2, data3, data4, text)
End Function
Sub useSquareInVBA()
MsgBox pll_dll_excel(3, 4, d1, d2, sometext)
Cells(5, 5).Value = d1
Cells(6, 6).Value = d2
Cells(7, 7).Value = sometext
End Sub
No, program halt, but sometext is always empty "" value.
Can you help me please?
I've checked with the article from here.
How to return a string of unknown size from DLL to Visual Basic
When I follow this article, I realized that is not "C". How do I make as a "C"?
I ran into a similar issue and this post is the most relevant search result.
Instead of giving a direct solution to the question (credit to GSerg for bringing out the correct approach), am posting a generic version of how I achieved receiving char array passed as reference:
C function (shared library):
int func(int a, double b, char *result) {
...
result[0] = 'A';
result[1] = 'B';
result[2] = '\0';
return EXIT_SUCCESS;
}
VBA:
Option Explicit
' Import DLL function
Private Declare PtrSafe Function func Lib "C:\path\to\c-shared-library.dll" _
(ByVal a As Integer, ByVal b As Double, ByVal result As String) As Integer
' Functions for use by Excel - DLL functions cannot be directly used
Function getResult(ByVal a As Integer, ByVal b As Double) As String
Dim status As Integer
Dim result As String
result = Space$(2)
status = func(a, b, result)
If status = 0 Then getResult = result Else Err.Raise 1000, "Error", "Failed to get result"
End Function
In my case the result are always going to be a two char array, but I think the above methodology can be accordingly modified.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.