473,698 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DLL export array reference to C#

115 New Member
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 8460
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
Thanks for the post, just gave it a try but still getting the same results :(
Mar 26 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Are you sure a String array is the same as an LPCTSTR?

I suspect interoperabilit y rules are causing this.

I did a Google advanced search using "interoperabili ty 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 New Member
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 Recognized Expert Moderator Expert
_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 MultibyteToWide Char 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 New Member
@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 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

2
2017
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. write the array to a CSV file 4. Go to the next row 5. Append the table row into the CSV file
10
1950
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 class template may be preceded by the export keyword. and from http://www.comeaucomputing.com/4.3.0/minor/win95+/43stuff.txt : A template must be declared export at both the point of definition and
205
10634
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
7788
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 will be a table containing transactions. I have a routine that uses a command button to set up the export and a module that takes care of the merge. I've scraped this together using posts on this group The command button portion works but I get...
11
2204
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 make: File, New, Project, Visual Basic Projects, Class Library, name of project: ComponentVB. We write the code: Public Function myFunction1(ByVal N_i As Integer, _
0
1565
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 following query which I am using to export data into CSV format;
5
3730
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 do this?
6
3585
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 the script in function. Then call out the function in PyCrust. But it's not convenient. And it causes global variable problem. The case is even I import the numpy lib in the function file.py, but it shows error: “NameError: global name 'append' is...
6
3508
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? I'm catching a System.EntryPointNotFoundException. dumpbin confirms that the Dll is not exporting the function.
0
8672
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
8600
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,...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8890
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
7711
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5859
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4360
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...
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
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.