473,799 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing string Array from unmanaged to managed code.

2 New Member
Hi, I have task I believe many people implemented many times.
The C++ application has to pass array of string to C# dll. To my surprise I have found very little about such stuff on the web.
I am able to pass single string, but when I try to pass SAFEARRAY* everything stops to work.
Please help.
To simplify my code I have a function which just pass array, but doesn’t do something with it.
If I commented out Safearray from function call it works and return that “105”. The problem with SAFEARRAY. Is it possible that dll expects some other format of array?
Expand|Select|Wrap|Line Numbers
  1. //dll
  2. public interface IEffDpthClc
  3.     {
  4. int SetCtArray(int NN, string[] CtFileArray);
  5.     }
  6.  
  7.    public class EffDpthClcClass : IEffDpthClc
  8.     {    
  9.     …..
  10.        public int SetCtArray(int NN , string[] CtFileArray)
  11.         {
  12.  
  13.             return 105;
  14.         }
  15.  
  16.     }
  17. //////////////////////////////
  18. //C++MFC  application
  19. HRESULT hr = CoInitialize(NULL);
  20. IEffDpthClcPtr pIEffDpthClc(__uuidof(EffDpthClcClass));
  21. SAFEARRAY *pSA;
  22. SAFEARRAYBOUND aDim[1];    
  23. aDim[0].lLbound= 0;  
  24. aDim[0].cElements=10;
  25. pSA= SafeArrayCreate(VT_VARIANT,1,aDim); 
  26. if (pSA != NULL)
  27.  
  28. {
  29. ConvertCArrayToSmartArr(pSA );
  30.        int z =pIEffDpthClc->SetCtArray(NN, pSA,p);
  31.        SafeArrayDestroy(pSA);
  32. }
  33. CoUninitialize();
  34.  
  35. ……
  36. HRESULT CPlan::ConvertCArrayToSmartArr(SAFEARRAY *pSA)
  37. {
  38.  
  39.         USES_CONVERSION; 
  40.         char buffer[10];  
  41.               HRESULT hr= S_OK;
  42.  
  43.             long aLong[1];
  44.  
  45. for (long i= 0; i< NN; i++)
  46.  {
  47.              VARIANT vOut;
  48.             VariantInit(&vOut);
  49.             vOut.vt= VT_BSTR
  50.             ltoa(i,buffer,10);  // convert long to ANSI string value
  51.             vOut.bstrVal= ::SysAllocString(A2W(buffer));                            aLong[0]= i;  // set index value
  52.             if (hr= SafeArrayPutElement(pSA, aLong, &vOut))
  53.                  {    // "correctly" copies VARIANT
  54.                         VariantClear(&vOut);  // release BSTR from memory on error
  55.                         SafeArrayDestroy(pSA); // does a deep destroy on error
  56.                         return hr;
  57.                 }
  58.                 VariantClear(&vOut);  // does a deep destroy of source VARIANT
  59.                } 
  60.  
  61. }
Thanks a lot.
Feb 10 '12 #1
1 3430
Elena Kr
2 New Member
Sorry.
My array was not good.
It should be

HRESULT hr= S_OK;

long aLong[1];

for (long i= 0; i< NN; i++)
{
CString cs(pArray->GetAt(i));
BSTR bstr = cs.AllocSysStri ng();
aLong[0]= i; // set index value
if (hr= SafeArrayPutEle ment(pSA, aLong,bstr))
{
::SysFreeString (bstr);
SafeArrayDestro y(pSA); // does a deep destroy on error
return hr;
}
::SysFreeString (bstr);

} // end iteration

Then it works.
Feb 13 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4657
by: Walter Zydhek | last post by:
Is it possible to convert an ArrayList to a string array? I have a routine that takes a string array as a parameter, converts it to an ArrayList for manipulation purposes, and now I need to convert it back to a string array. example of code I am using: Sub MakeChanges(byval strChange() as string) as string() Dim aryValue As ArrayList = New ArrayList(strChange)
3
2885
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I call the function using this code fragement in my main function: --- char** arg_array; arg_count = create_arg_array(command, argument, arg_array); for(count = 0; count < arg_count; count++)
1
3953
by: VCVCVC VC via DotNetMonster.com | last post by:
Hi , I want to pass a string array to an unmanaged COM Componet. The component should retrieve it. Pls help me to understand with a sample code. I have declared a BSTR* myStringarr, for this in the COM component. How to retrieve values from this myStringarr? -- Message posted via http://www.dotnetmonster.com
2
5270
by: Herbert VON GRÜNENWALD | last post by:
i'v got: public static extern int ReadFile( IntPtr hFile, IntPtr lpBuffer, uint nNumberOfBytesToRead, ref uint lpNumberOfBytesRead, uint lpOverlapped);
0
1703
by: nygiantswin2005 | last post by:
I would like to know how do I pass a pointer to a struct from managed code to unmanaged code. For example if I create structure like this in managed code. StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi ) ] public struct MYSTRUCT { public String Text;
3
2298
by: Boni | last post by:
Dear all, I have written a C++ com object: STDMETHOD(myfunc)(INT* array) Now I need to pass an array from c# to com. I generated a COM interop and it the signature of the function is myfunc(ref int)
5
7808
by: Jackson | last post by:
I have something that is stumping me. I am trying to initialize a 3 dimensional string array with the code below, but it wont compile. Can anyone explain what Im doing wrong?????????????? Im going crazy with this one! Ive tried a 2 dimensional array minus the "test3" and it works fine. public string screens; screens = new string{ {"test1","test2","test3"} };
1
1833
by: matthew breedlove | last post by:
I have a Managed VC++ WinForms app in VC8 calling a member function of a class in a native DLL. My managed code is similar to this: System::Void btnTest_Click(System::Object^ sender, System::EventArgs^ e) { Receiver r; std::vector<char> v; v.push_back('A');
6
8016
by: Ananas | last post by:
Hi, My native C++ function creates a dynamic array. I'm marshalling it to managed code and got to delete after. How to make it: c++ code: void CreateArrayInside( pTestStruct &TestStruct, unsigned int &size) {
17
7258
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.