1

I have some sample code from MSDN that I am trying to adapt for use but the VBA compiler rejects the contents of the angled brackets < >. I have the following code in a module:

Imports System

Imports System.Runtime.InteropServices


<DllImport("../../insert_dll_name_here.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Function Test(file() As String) As Integer
End Function

I am trying to use this code to call a simple function from a C++ dll that expects an array of strings but I get the compile error 'expected line number or label or statement or end of statement' and do not find the help menu provided to be any use. I have tried square brackets [ ] in case this is a problem of VBA version to no avail. Could someone point out my error in using the COM interop services.

1 Answer 1

1

The code is VB.NET. This is not VBA.

In VBA you would write,

Declare Function Test Lib "../../insert_dll_name_here.dll" (file() As String) As Long

However, VBA does not directly support the cdecl convention, but you can make it work with a type library. You may also have troubles with the file() As String array - make sure to handle it properly on the C++ side.

As a side note, this has nothing to do with COM. This is calling a function from an external library.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, you're right, clearly my ability to read MSDN threads on Monday morning is questionable. So is it easier to use a type library to link to this piece of VB.net or just look for an alternative method of calling a C++ dll function that expects an array of strings in your opinion?
@OOhay "Easier" is very subjective here. You don't use a TLB to link to this VB.NET code; using a TLB means you're linking to the library directly, and you will have to make sure you understand the string array/SAFEARRAY business on both ends. Also, you won't be able to debug, the IDE will crash if you try. Alternatively, you can create a VB.NET COM-visible class library that exposes a COM wrapper that calls the library functions. In this case you will have the additional wrapper DLL to carry along and register, but it will be easy to use from VBA (a single call to CreateObject).
Thanks for the complete answer, as you might have guessed, I know little about VBA ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.