473,396 Members | 2,039 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,396 software developers and data experts.

trouble using unmanaged c++ from c# using dllimport

84
Hi guys,
i am having trouble importing c++ unmanaged dll into C# [winform]. Can someone help?
Basically i am just trying to create a safearray of strings in c++ and trying to send it to C#.

Here is my c++ code.
Expand|Select|Wrap|Line Numbers
  1. extern "C" __declspec(dllexport) BOOL GetStringArr(SAFEARRAY* arr)
  2. {
  3. SAFEARRAY*    myArray;
  4.   SAFEARRAYBOUND  rgsabound[1];
  5.  
  6.   rgsabound[0].lLbound = 0;
  7.   rgsabound[0].cElements = 5;
  8.  
  9.   myArray = SafeArrayCreate(VT_BSTR, 1, rgsabound);
  10.   VARIANT* pvData = (VARIANT*)(myArray->pvData);
  11.  
  12.   pvData[0].vt = VT_BSTR;
  13.   pvData[0].bstrVal = SysAllocString(L"FirstString");
  14.   pvData[1].vt = VT_BSTR;
  15.   pvData[1].bstrVal = SysAllocString(L"SecondString");
  16.   pvData[2].vt = VT_BSTR;
  17.   pvData[2].bstrVal = SysAllocString(L"ThirdString");
  18.   pvData[3].vt = VT_BSTR;
  19.   pvData[3].bstrVal = SysAllocString(L"FourthString");
  20.   pvData[4].vt = VT_BSTR;
  21.   pvData[4].bstrVal = SysAllocString(L"FifthString");
  22.  
  23.   arr = myArray;
  24.   return true;
  25. }
  26.  

Here is my c# code.
Expand|Select|Wrap|Line Numbers
  1. [DllImport("MyData.dll", EntryPoint = "GetStringArr")]
  2. public static extern bool GetStringArr([MarshalAs(UnmanagedType.SafeArray)] out Array strServerList); 
  3.  
i am getting exception when i call GetStringArr from C#. i am sure there is something silly i am doing. Can someone please help?

Thanks in advance.
Sep 7 '10 #1
1 1486
Plater
7,872 Expert 4TB
What is the exact exception that you get?
Sep 9 '10 #2

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

Similar topics

5
by: Jason Bell | last post by:
I'm an experienced programmer, but a newbie to c#, so please be kind :) In a c++, unmanaged, DLL of my own making I have a function called void XGL_Core_SetCaption(XGL_Core * core, char *...
2
by: Alex Sedow | last post by:
Hello group! How to load assembly from GAC using unmanaged C++? I want load assembly into memory or get path to file on disk. Alex.
7
by: Kristof Thys via .NET 247 | last post by:
Post a new message to microsoft.public.dotnet.languages.vc http://www.dotnet247.com/247reference/default.aspx Hello, I've been struggling for weeks with this problem, I hope I find some...
1
by: RaKKeR | last post by:
Hi, I'm trying to wrap my unmanaged c++ code into a managed c++ wrapper. The problem is VC .NET doesn't seem to find the namespaces defined in my unmanaged code. Let's say I have a file...
2
by: Mayur | last post by:
I tried followinf but working fine fo int but how to do it for string using user custome message in c# public static extern int FindWindow(string strClassName,string strWindowName);
2
by: =?Utf-8?B?Tm9tYW4gQWxp?= | last post by:
Hi, We are facing a strange problem in our ASP. NET website. Some times it gives the following unhandled exception. Error Message: Exception of type System.Web.HttpUnhandledException was...
0
by: stacionari | last post by:
HI, My query is like this: SELECT tag.* FROM tag LEFT JOIN book_item ON tag.Tag_ID = book_item.BookItem_ID WHERE tag.Tag = "tags" GROUP BY tag.tag_ID Indexes: Table 'tag': Keyname Type...
1
by: =?Utf-8?B?UnVpIE9saXZlaXJh?= | last post by:
I am trying to use an unmanaged C++ OCX in a C#.NET application. This is generating the following error: “Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))” If I use...
0
by: Basha J P M | last post by:
I am beginner in python. I am working through the tutorial examples from http://www.swig.org/ and have run into some problems. I took the following command instructions from the tutorial on...
0
by: satyasri2test | last post by:
Hi, Can some one provide me the complete example for using FILTER USING option in CREATE FUNCTION (External Scalar) for DB2 LUW 9.5. I have verified the sample given in the SQL Reference Volume2,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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
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
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...
0
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...

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.