Problem in Passing 1d array from VC++ to VB [modified]
-
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
-
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
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,
-
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
Hello, Read the article on MSDN: “How to Pass Arrays between Visual Basic and C”. http://support.microsoft.com/kb/207931 Regards
-
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
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
-
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,
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