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? -
//dll
-
public interface IEffDpthClc
-
{
-
int SetCtArray(int NN, string[] CtFileArray);
-
}
-
-
public class EffDpthClcClass : IEffDpthClc
-
{
-
…..
-
public int SetCtArray(int NN , string[] CtFileArray)
-
{
-
-
return 105;
-
}
-
-
…
-
}
-
//////////////////////////////
-
//C++MFC application
-
HRESULT hr = CoInitialize(NULL);
-
IEffDpthClcPtr pIEffDpthClc(__uuidof(EffDpthClcClass));
-
SAFEARRAY *pSA;
-
SAFEARRAYBOUND aDim[1];
-
aDim[0].lLbound= 0;
-
aDim[0].cElements=10;
-
pSA= SafeArrayCreate(VT_VARIANT,1,aDim);
-
if (pSA != NULL)
-
-
{
-
ConvertCArrayToSmartArr(pSA );
-
int z =pIEffDpthClc->SetCtArray(NN, pSA,p);
-
SafeArrayDestroy(pSA);
-
}
-
CoUninitialize();
-
-
……
-
HRESULT CPlan::ConvertCArrayToSmartArr(SAFEARRAY *pSA)
-
{
-
-
USES_CONVERSION;
-
char buffer[10];
-
HRESULT hr= S_OK;
-
-
long aLong[1];
-
-
for (long i= 0; i< NN; i++)
-
{
-
VARIANT vOut;
-
VariantInit(&vOut);
-
vOut.vt= VT_BSTR
-
ltoa(i,buffer,10); // convert long to ANSI string value
-
vOut.bstrVal= ::SysAllocString(A2W(buffer)); aLong[0]= i; // set index value
-
if (hr= SafeArrayPutElement(pSA, aLong, &vOut))
-
{ // "correctly" copies VARIANT
-
VariantClear(&vOut); // release BSTR from memory on error
-
SafeArrayDestroy(pSA); // does a deep destroy on error
-
return hr;
-
}
-
VariantClear(&vOut); // does a deep destroy of source VARIANT
-
}
-
-
}
Thanks a lot.
1 3430
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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++)
|
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
|
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);
|
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;
| |
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)
|
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"} };
|
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');
|
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)
{
|
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?
|
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...
| |
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,...
|
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,...
|
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...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |