473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DLL export array reference to C#

115 100+
I am trying to pass a string array from c# into c++ and manipulate the data of that array. How can I do this? (I am c++ newb)

c++
Expand|Select|Wrap|Line Numbers
  1. __declspec(dllexport) int __stdcall Test(LPCTSTR* as_test)
  2. {
  3.     as_test[0] = L"This is new data";
  4.                 return 0;
  5. }
c#
Expand|Select|Wrap|Line Numbers
  1. [DllImport("PBNI.dll", CharSet = CharSet.Unicode)]
  2.             static extern int Test(ref String[] as_ret);
  3.  
  4.        public void Testing()
  5.        {
  6.                 String[] ls = new String[] { "replace me", "more data", "even more" };
  7.                 Test(ref ls);
  8.                 Console.WriteLine(ls[0]);
  9.        }
I tried this, but I get two japanese chars..
Mar 25 '09 #1
10 8417
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  as_test[0] = L"This is new data"; 
as_test is an LPCTSTR* but L"This is new data" is not a Unicode string.

You might try:

Expand|Select|Wrap|Line Numbers
  1.  as_test[0] = TEXT("This is new data"); 
Mar 25 '09 #2
ShadowLocke
115 100+
Thanks for the post, just gave it a try but still getting the same results :(
Mar 26 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Are you sure a String array is the same as an LPCTSTR?

I suspect interoperability rules are causing this.

I did a Google advanced search using "interoperability msdn String" and got a lot of hits. One of them said:

By default, .NET strings are marshalled by COM Interop to LPTSTR in C++ and, vice versa, and so you have to explicitly marshal any other type of unmanaged string (including BSTR) to and from a .NET string using the MarshalAs attribute.

There's no particular problem in inheriting from IUnknown or IDispatch but, if you inherit from the latter, you have to mark the C# interface declaration with the InterfaceType attribute.
I am leaving it to you to research this.
Mar 27 '09 #4
ShadowLocke
115 100+
I've gotten a step closer by changing the c++ code to this..

Expand|Select|Wrap|Line Numbers
  1. __declspec(dllexport) int __stdcall Test(LPCWSTR * &as_test)
  2.     _tcscpy((wchar_t*)as_test[0], L"new data");
  3.  
  4.     return 0;
  5. }
But this replaces the string array with an array of the 1 element that was changed..I dont know why im losing the rest of the data
Mar 30 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
_tcscpy is a TCHAR macro that resolves to either strcpy(ASCII) or wcscpy(Unicode). That means you need an LPCTSTR argument.

Then L"new data" does not produce the correct string. Use the TEXT or _T macro. This will call MultibyteToWideChar on the Unicode side.
Expand|Select|Wrap|Line Numbers
  1. __declspec(dllexport) int __stdcall Test(LPCTSTR as_test) 
  2. {  
  3.     _tcscpy(as_test, TEXT("new data"); 
  4.  
  5.     return 0; 
I know you are passing in an array of C# strings but I suggest you pass in onyl one string until you get a result.

Next, _tcscpy does not check that you have enough memory to make a copy. Plus the address is the address of the C# string. That causes _tcscpy to overwrite the C# string and you have no idea what the format of a C# string object is. Therefore, I expect corruption here.

I don't see where you are marshaling the code on the C++ side and I don't see you using IDispatch on the C++ side. That is, COM does your marshaling aand it does it through IDispatch.

You can't simply copy from one language to the other.
Apr 1 '09 #6
ShadowLocke
115 100+
@weaknessforcats
@ShadowLocke
Taking the array element out of the picture everything works as expected. I'm getting results, just not what im asking for. I need to return an edited array parameter..

Passing the array of strings, in the c++ code I am able to messagebox(0, as_test[any index], L"", 0) and see that each string is indeed there. So the pointer in is working.

I think i will end up turning the array into a delemited string but that just seems wrong..

@weaknessforcats
This is a test. I want to see results before I make it error proof.

@weaknessforcats
I know nothing of this IDispatch, please elaborate if you still think your on the right track. And since I see the data coming in properly, I think my problem here is arrays. Not strings.

...
You can't simply copy from one language to the other.
Hence...the orginal cry for help.
Apr 2 '09 #7
ShadowLocke
115 100+
Problem solved with this:

c++
Expand|Select|Wrap|Line Numbers
  1. __declspec(dllexport) int __stdcall Test(LPCTSTR ** as_test)
  2. {
  3.     _tcscpy((wchar_t*)as_test[0], TEXT("a result."));
  4.  
  5.     return 3;
  6. }
c#
Expand|Select|Wrap|Line Numbers
  1. [DllImportAttribute("PBNI.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
  2.             public static extern void Test([In, Out] String[] as_test);
Arrays require the [In, Out] attributes to come back.
Apr 3 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
Your solution fails if your code is compiled using ASCII.

Also, it assumes a pointer to a pointer is the same as a pointer. And it assumes there is enough allocated memory at that location to contain the source string. And it assumes a C# String has the same format as a C string considering you are overwriting the C# string.

You need a solution that does not rely on a cast and that works for both ASCII and Unicode.
Apr 4 '09 #10
ShadowLocke
115 100+
@weaknessforcats
One..the question was about passing arrays from c# to c++ and back..not memory checking and error proofing..that is something do be done after I figured out the problem at hand.. Two..reading the material that you gave (after digging further around in to find something that remotly addressed the issue at hand [http://msdn.microsoft.com/en-us/library/hk9wyw21.aspx]) makes the same assumption about strings (Im gonna go ahead and take MSDN's word on it).. Three..thanks for telling me what i need on my current very specific project where i know exactly what is going into the function that i create and what is not (i.e. This function will always receive unicode from me..so sence it will never be compiled in ascii..why would i do extra work to account for it??). I honestly did not know what my project was meant to do until you told me.....

Stop tying to answer questions that are not asked...it just confuses bystanders
Apr 7 '09 #11

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

Similar topics

2
by: Andy Jacobs | last post by:
Hi all I am looking at the fputcsv function for something that I need. In summary, this is what I want to do: 1. Go through a table 2. Get all the columns and put them into an array 3. ...
10
by: New_user | last post by:
Hello. Excerpt from ISO/IEC 14882 : 14/6 A namespace-scope declaration or definition of ........skipped...... a non- inline member function of a class template or a static data member of a...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
1
by: Michael | last post by:
I have an application that will export two files to fixed width text to combine as a single text file. The first export will be a query containing header information for the file, the second query...
11
by: Dr. Zharkov | last post by:
We want to export myArrayVB (2000, 2) of VB .NET 2003 in myArrayVó of VC++ .NET 2003 on scheme "component - client". But there is an error. For development of a component in VB .NET 2003 we...
0
by: mathewbutler | last post by:
Access 2000 SR-1 I've spent some time investigating this, and researching both here and on the MS support site. Briefly; Problem Description: ------------------------------- I have the...
5
by: michealmess | last post by:
Can anyone help me. I wish to export an array from vb.net to excel ranges. This will happen for multiple files. The ranges names will not be start and end in the same cells on all files. How do i...
6
by: Kanis | last post by:
Hi, I am a Python beginner and using PyCrust to process data for my research work. I found I not able to import a .py file directly. But I have to type in every lines every time. Or I can save...
6
by: Jeffrey Walton | last post by:
Hi All, Sorry about dropping thish on M.P.D.L.CSharp. There is no M.P.D.L.VC. So I hope someone has come across the issue... How does one export a function (not a class) in a managed Dll? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.