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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
OOhay
  • 61
  • 6

1 Answers1

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.

Community
  • 1
  • 1
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • 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 May 20 '13 at 11:35
  • @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`). – GSerg May 20 '13 at 11:42
  • Thanks for the complete answer, as you might have guessed, I know little about VBA ;) – OOhay May 20 '13 at 11:45