-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to reference DLL created with DllExport into Excel VBA #189
Comments
Hello,
What error?
I only have LibreOffice to test something but I'm not sure about compatibility or similarity to MS products regarding VBA. Anyway, did you try something from other issues about VBA https://github.com/3F/DllExport/issues?q=is%3Aissue+vba |
Hi, thank you for your reply. The error I am getting is the following, along with the code I am using to call the method from the VB.NET dll: Why can't VBA find the entry point? Am I missing anything? I read the other threads you linked me, but none reported the same error I am having. Plus, they are about C#, not VB.NET. Would you mind providing some assistance? I would be very grateful |
C# and VB.NET both are CLR based thus it does not matter because it just frontend.
What result for the following command ?
|
@supertrip86 I'm a little confused with your code. Here you're trying to call But your initial code just declares a simple static function as <DllExport>
Public Shared Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC(ByRef DATA_RNG As Double(,))
So, I'm not really sure what are you trying here. Can you attach any complete src or project example? Isn't this is what you need ? Declare Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC Lib ... Also, About ByRef. Do not pass arrays like here. Either configure marshaling or use pointer to allocated region; maybe manually or using something like Conari etc. Please read this https://github.com/3F/DllExport/wiki/Quick-start#about-data-types
I suggest you MSDN or any Dev Q&A such as stackoverflow |
Hi, thank your your patience. What I need is to be able to call several functions from the MonteCarlo DLL within VBA (MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC is only one of those functions). Nothing more than that. So far, I only added MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC as a static method, just to to see if I can manage to call it from within VBA. If I succeed, I will add the rest. Now, I followed your advice and tried the code below, but if I run it all Excel crashes... No errors are provided, the entire Excel freezes and I have to manually terminate it.
Is it because I am using ByRef? I understand you adviced me not to use it. Is this the reason? |
Quite possible
So what exactly, crashes or freezes ? In any case just marshal it correctly IF it cannot be marshaled automatically by CLR. MarshalAs attribute, or custom implementation around Marshal.Alloc and so on |
As you can see from the screenshot below, MS Excel and the VBA editor gets stuck (not responding), and I have to terminate them. Here is the log from the Data tab:
If you confirm there is nothing else that can be wrong, I will start studying about marshaling using the link you provided me. Many thanks! |
I don't see the problems for your configuration and used environment. Did you try configure marshaling at least by using MarshalAs attribute as said above? For better understanding try define ranges manually without For/UBound, etc. Try to keep everything as simple as possible in order to debug something. I am not guru in VBA (last time I used this seems in ~2006) and I don't have MS Excel on my machines to debug your script at runtime. But definitely For+UBound are not safe in your code as I can see here. This can lead to a moving out of bounds of the allocated memory region due to incorrect marshaling (affects entirely incorrect values and addresses) etc. Which may produce exactly some freezes, I think. Also, what about VBA's multidimensional ByRef along with LongPtr to a single-dimensional array def in a CLR side, like Declare PtrSafe Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC Lib "C:\path\to\MonteCarlo.dll"
(ByRef DATA_RNG As LongPtr) As Double Public Shared Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC(
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.R8, SizeConst:=4)> ByVal r As Double()) As Double
Dim DATA_RNG As Double(,) = { {r(0), r(1)}, {r(2), r(3)}}
' DATA_RNG(0, 1)
' DATA_RNG(1, 0)
Definitely a huge recommendation in any case since mechanism will be similar even between other languages. |
Hi, thank you again for your patience. These are the modifications I made to my code. I also removed UBound, to see if there could be improvements.
And here is VBA:
Not sure why, even though I declared DATA_RNG as LongPtr, it gives me "data mismatch"... |
Try Dim DATA_RNG(2, 2) As Double
' ...
result = MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC(DATA_RNG) continue to keep ByRef LongPtr + Marshalable ByVal r As Double declaration Declare PtrSafe ... (ByRef DATA_RNG As LongPtr) As Double Public Shared Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC(
<MarshalAs(UnmanagedType.LPArray,...> ByVal r As Double()) As Double I'm not sure about VBA but without COM you need obviously pass it as address to its allocated memory in the heap. But after, CLR will marshal ByVal Double according some information, for example, configured through MarshalAs attribute. There, however, you can also marshal it independently through some other technic but I think for VBA this will be hard. Alternatively you can also try to get address to allocated DATA_RNG manually to work only with LongPtr/IntPtr but this is another level for ascetic VBA. |
@supertrip86, You have two options:
|
. . .
The question is related to:
DllExport - latest
:Hi, my goal is to create a windows unmanaged DLL to be loaded into Excel VBA, without the need to select "Register for COM Interop" in Visual Studio, and without having to use regasm to register the DLL. This because the DLL I intend to create needs to be deployed on a machine where I cannot install Visual Studio, and where I probably won't have administrator's privileges.
I have a single project in VB.NET, where I have installed the DllExport NuGet package, and set "x86" anywhere in my solution rather than "AnyCPU".
Here is the code:
`
`
I followed the indications in the first 1:34 minutes of this video:
https://www.youtube.com/watch?v=sBWt-KdQtoc
And then built the solution. No errors till this point.
Eventually, I added this delcaration on top of a VBA module:
Public Declare Function MATRIX_ELEMENTS_CUMULATIVE_SUM_FUNC Lib "C:\path\to\file.dll" (ByRef DATA_RNG As Variant)
I am trying to test this function, but I can't seem to be able to make it work.
Unfortunately I have no experience with VBA, can anyone tell me what am I doing wrong?
The text was updated successfully, but these errors were encountered: