Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. Problem in Passing 1d array from VC++ to VB [modified]

Problem in Passing 1d array from VC++ to VB [modified]

Scheduled Pinned Locked Moved COM
helpc++comdata-structuresquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    georgekjolly
    wrote on last edited by
    #1

    Hi all, I have a VB applicatiOn , which calls a function in VC++ DLL. Arguement of function is a one dimensional string array. Array will get filled in VC++ dll function. I have written the programme given below. But I am unable to get the array elements from VC++. Programme is given below. VC++ COM /*****************************************/ STDMETHODIMP CTest111CL::myFun(SAFEARRAY **ppOutputArray) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) HRESULT hresult; SAFEARRAYBOUND aDim[1] ; aDim[0].lLbound = 0; aDim[0].cElements = 3; BSTR HUGEP *pbstr; *ppOutputArray = SafeArrayCreate(VT_BSTR , 1, aDim); hresult=SafeArrayAccessData(*ppOutputArray,(void HUGEP**)&pbstr); pbstr[0] = SysAllocString(OLESTR("SOURCE_VA")); pbstr[1] = SysAllocString(OLESTR("ACV")); pbstr[2] = SysAllocString(OLESTR("10")); SafeArrayUnaccessData(*ppOutputArray); return S_OK; } /**********************************************/ VB Application --------------- '''''''''''''''''''''''''''' Dim i As Test111CL Private Sub Command1_Click() Dim arr_str(3) As String Set i = New Test111CL i.myFun arr_str() MsgBox arr_str(1) End Sub '''''''''''''''''''''''''' MsgBox arr_str(1) , The message Box shows and empty string. How can I rectify it. Please help me. Thanks George -- modified at 4:53 Friday 9th February, 2007

    L 3 Replies Last reply
    0
    • G georgekjolly

      Hi all, I have a VB applicatiOn , which calls a function in VC++ DLL. Arguement of function is a one dimensional string array. Array will get filled in VC++ dll function. I have written the programme given below. But I am unable to get the array elements from VC++. Programme is given below. VC++ COM /*****************************************/ STDMETHODIMP CTest111CL::myFun(SAFEARRAY **ppOutputArray) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) HRESULT hresult; SAFEARRAYBOUND aDim[1] ; aDim[0].lLbound = 0; aDim[0].cElements = 3; BSTR HUGEP *pbstr; *ppOutputArray = SafeArrayCreate(VT_BSTR , 1, aDim); hresult=SafeArrayAccessData(*ppOutputArray,(void HUGEP**)&pbstr); pbstr[0] = SysAllocString(OLESTR("SOURCE_VA")); pbstr[1] = SysAllocString(OLESTR("ACV")); pbstr[2] = SysAllocString(OLESTR("10")); SafeArrayUnaccessData(*ppOutputArray); return S_OK; } /**********************************************/ VB Application --------------- '''''''''''''''''''''''''''' Dim i As Test111CL Private Sub Command1_Click() Dim arr_str(3) As String Set i = New Test111CL i.myFun arr_str() MsgBox arr_str(1) End Sub '''''''''''''''''''''''''' MsgBox arr_str(1) , The message Box shows and empty string. How can I rectify it. Please help me. Thanks George -- modified at 4:53 Friday 9th February, 2007

      L Offline
      L Offline
      lafleon
      wrote on last edited by
      #2

      Hello, Try this: Idl: HRESULT FunName([in,out] SAFEARRAY(BSTR)* pbsData); .h: STDMETHOD(FunName)((SAFEARRAY** pbsData); .cpp: STDMETHODIMP ClassName:: FunName(SAFEARRAY** pbsData) { //checking if it is an one-dimensional array if((*pbsData)->cDims != 1) return E_FAIL; //Checking the number of elements of the array if(3 != (*pbsData)->rgsabound[0].cElements) return E_FAIL; // locking the array before using its elements HRESULT hr = SafeArrayLock(*pbsData); if(FAILED(hr)) return E_FAIL; // using the array BSTR* pbs = (BSTR*)(*pbsData)->pvData; pbs[0] = … pbs[1] = … pbs[2] = … … // releasing the array return SafeArrayUnlock(*pbsData); } VB: Option Explicit Option Base 1 Dim sArr(1 To 3) As String Call ….FunName(sArr) Regards,

      G 1 Reply Last reply
      0
      • G georgekjolly

        Hi all, I have a VB applicatiOn , which calls a function in VC++ DLL. Arguement of function is a one dimensional string array. Array will get filled in VC++ dll function. I have written the programme given below. But I am unable to get the array elements from VC++. Programme is given below. VC++ COM /*****************************************/ STDMETHODIMP CTest111CL::myFun(SAFEARRAY **ppOutputArray) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) HRESULT hresult; SAFEARRAYBOUND aDim[1] ; aDim[0].lLbound = 0; aDim[0].cElements = 3; BSTR HUGEP *pbstr; *ppOutputArray = SafeArrayCreate(VT_BSTR , 1, aDim); hresult=SafeArrayAccessData(*ppOutputArray,(void HUGEP**)&pbstr); pbstr[0] = SysAllocString(OLESTR("SOURCE_VA")); pbstr[1] = SysAllocString(OLESTR("ACV")); pbstr[2] = SysAllocString(OLESTR("10")); SafeArrayUnaccessData(*ppOutputArray); return S_OK; } /**********************************************/ VB Application --------------- '''''''''''''''''''''''''''' Dim i As Test111CL Private Sub Command1_Click() Dim arr_str(3) As String Set i = New Test111CL i.myFun arr_str() MsgBox arr_str(1) End Sub '''''''''''''''''''''''''' MsgBox arr_str(1) , The message Box shows and empty string. How can I rectify it. Please help me. Thanks George -- modified at 4:53 Friday 9th February, 2007

        L Offline
        L Offline
        lafleon
        wrote on last edited by
        #3

        Hello, Read the article on MSDN: “How to Pass Arrays between Visual Basic and C”. http://support.microsoft.com/kb/207931 Regards

        1 Reply Last reply
        0
        • G georgekjolly

          Hi all, I have a VB applicatiOn , which calls a function in VC++ DLL. Arguement of function is a one dimensional string array. Array will get filled in VC++ dll function. I have written the programme given below. But I am unable to get the array elements from VC++. Programme is given below. VC++ COM /*****************************************/ STDMETHODIMP CTest111CL::myFun(SAFEARRAY **ppOutputArray) { AFX_MANAGE_STATE(AfxGetStaticModuleState()) HRESULT hresult; SAFEARRAYBOUND aDim[1] ; aDim[0].lLbound = 0; aDim[0].cElements = 3; BSTR HUGEP *pbstr; *ppOutputArray = SafeArrayCreate(VT_BSTR , 1, aDim); hresult=SafeArrayAccessData(*ppOutputArray,(void HUGEP**)&pbstr); pbstr[0] = SysAllocString(OLESTR("SOURCE_VA")); pbstr[1] = SysAllocString(OLESTR("ACV")); pbstr[2] = SysAllocString(OLESTR("10")); SafeArrayUnaccessData(*ppOutputArray); return S_OK; } /**********************************************/ VB Application --------------- '''''''''''''''''''''''''''' Dim i As Test111CL Private Sub Command1_Click() Dim arr_str(3) As String Set i = New Test111CL i.myFun arr_str() MsgBox arr_str(1) End Sub '''''''''''''''''''''''''' MsgBox arr_str(1) , The message Box shows and empty string. How can I rectify it. Please help me. Thanks George -- modified at 4:53 Friday 9th February, 2007

          L Offline
          L Offline
          lafleon
          wrote on last edited by
          #4

          Some more links: 1. "Passing an array from VC++ DLL to VB", by Amol Kakhandki on Code project. http://www.codeproject.com/dll/ctovbarray\_passing.asp 2. http://www.manbu.net/Lib/En/Class5/Sub7/1/23.asp

          1 Reply Last reply
          0
          • L lafleon

            Hello, Try this: Idl: HRESULT FunName([in,out] SAFEARRAY(BSTR)* pbsData); .h: STDMETHOD(FunName)((SAFEARRAY** pbsData); .cpp: STDMETHODIMP ClassName:: FunName(SAFEARRAY** pbsData) { //checking if it is an one-dimensional array if((*pbsData)->cDims != 1) return E_FAIL; //Checking the number of elements of the array if(3 != (*pbsData)->rgsabound[0].cElements) return E_FAIL; // locking the array before using its elements HRESULT hr = SafeArrayLock(*pbsData); if(FAILED(hr)) return E_FAIL; // using the array BSTR* pbs = (BSTR*)(*pbsData)->pvData; pbs[0] = … pbs[1] = … pbs[2] = … … // releasing the array return SafeArrayUnlock(*pbsData); } VB: Option Explicit Option Base 1 Dim sArr(1 To 3) As String Call ….FunName(sArr) Regards,

            G Offline
            G Offline
            georgekjolly
            wrote on last edited by
            #5

            Hi lafleon , Its working excellent . I have implemented it for a 3d array and got correct result. Thank you Very much for your help. George

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups