Thread: Passing an array of strings from VB to C++

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Passing an array of strings from VB to C++

    Hi, I have my C++ compiled as a DLL, and I need to call a function that expects an array of character arrays (that is, an array of strings, but not the STL string). How can I do this in VB?

    The function template is as follows:
    Code:
    int __stdcall startFunction(int numScenes, char** scenes, char* dest);
    The problem is I don't know how to pass the "scenes" variable. Please help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Whatever the answer, it's going to be windows specific.

    How about
    http://www.google.co.uk/search?q=vb+calling+c%2B%2B
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    not sure but have you looked at the VARIANT data type...
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It's doable, but not that nice.

    C++ Dll Header
    Code:
    #include "stdafx.h"
    #include <oleauto.h>
    
    #define DLLEXPORT extern "C" __declspec(dllexport)
    
    DLLEXPORT void ReadOutArrayOfStrings(LPSAFEARRAY* StringArray);
    C++ Main File
    Code:
    #include "stdafx.h"
    #include "MyC++Dll.h"
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    	case DLL_THREAD_ATTACH:
    	case DLL_THREAD_DETACH:
    	case DLL_PROCESS_DETACH:
    		break;
    	}
        return TRUE;
    }
    
    
    DLLEXPORT void ReadOutArrayOfStrings(LPSAFEARRAY* StringArray)
    {
    	char** StrPtr = 0;
    	long LowerBound = 0;
    	long UpperBound = 0;
    
    	SafeArrayGetLBound(*StringArray,1,&LowerBound);
    	SafeArrayGetUBound(*StringArray,1,&UpperBound);
    
    	SafeArrayAccessData(*StringArray, reinterpret_cast<void**>(&StrPtr));
    
    	for(int i = LowerBound; i <= UpperBound;++i)
    		MessageBox(HWND_DESKTOP,StrPtr[i],"From VB",MB_OK);
    
    	SafeArrayUnaccessData(*StringArray);
    
    }
    VB Form Code
    Code:
    Private Declare Sub ReadOutArrayOfStrings _
        Lib "MyC++Dll.dll" (theArray() As String)
    
    
    Private Sub Command1_Click()
        Dim StringArray(9) As String
        StringArray(0) = "All"
        StringArray(1) = "Work"
        StringArray(2) = "And"
        StringArray(3) = "No"
        StringArray(4) = "Play"
        StringArray(5) = "Makes"
        StringArray(6) = "Jack"
        StringArray(7) = "A"
        StringArray(8) = "Dull"
        StringArray(9) = "Boy"
        
        ReadOutArrayOfStrings StringArray
        
    End Sub

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    2
    OK Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. Passing Array of Strings to a function ??
    By Taper in forum C Programming
    Replies: 20
    Last Post: 12-07-2008, 02:58 PM
  3. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  4. Passing a pointer to two-dimension array in a function
    By E_I_S in forum C++ Programming
    Replies: 11
    Last Post: 06-19-2008, 09:57 AM
  5. problem with an array of strings in C
    By ornamatica in forum C Programming
    Replies: 14
    Last Post: 05-01-2002, 06:08 PM