Click here to Skip to main content
15,179,246 members
Articles / Programming Languages / C#
Posted 1 Nov 2006


28 bookmarked

Call C# code from C++ and read an array of struct which contains strings

Rate me:
Please Sign up or sign in to vote.
4.05/5 (13 votes)
1 Nov 2006CPOL2 min read
How to call C# code from C++ and read an array of struct which contains strings.


My C++ code was trying to read an array of struct which contains string data in C#. To my surprise, I found that a struct array which contains only integers is very easy to get, but if you have a string in the array, you need to do some more things. I searched on Google, and a lot of people were talking about the same problem and getting an exception which says, "Old format or invalid type library". But no where was I able to find the answer, and since I was able to solve the problem myself, I am publishing the solution here.

Basic Ideas

I have a C# DLL which contains a struct; let's say:

public struct MyStruct
    public string name;
    public string surname;
    public int age;

I have an interface which looks like this:

public interface ITest
    MyStruct[] GetData        

And my main class is:

public class Test : ITest
    MyStruct[] st = new MyStruct[2];
    public Test()
        st[0].name = "abc";
        st[0].surname = "def";
        st[0].age = 10;
        st[1].name = "qwe";
        st[1].surname = "rty";
        st[1].age = 20;
    public MyStruct[] GetData
            return st;

Now build the DLL. Then, from the Visual Studio ommand prompt, type type "regasm MyInterOp.dll /tlb:MyInterOp.tlb". Have look at the tlb using OleViewer. Find the tag MyStruct. It will have an LPSTR.

Now create a console application in C++ like this:

HRESULT hr = CoInitialize(NULL);
ITest* pTest = NULL;
hr = CoCreateInstance(__uuidof(Test),NULL, 
MyInterOp::MyStruct HUGEP *pBSTR;
hr = SafeArrayAccessData(pTest->GetData, (void HUGEP* FAR*)&pBSTR);
printf("Name: %S \n",pBSTR[0].name);
printf("SurName: %S \n",pBSTR[0].surname);
printf("Age: %d \n",pBSTR[0].age);
printf("Name: %S \n",pBSTR[1].name);
printf("SurName: %S \n",pBSTR[1].surname);
printf("Age: %d \n",pBSTR[1].age);

When you run this application, it will give you an exception. If you debug it, you can see that the HRESULT is "-2147319783" which means "Old format or invalid type library." So LPSTR is not going to work for us.


How can we solve this issue? Make your struct look like this:

public struct MyStruct
    public string name;
    public string surname;
    public int age;

Register the DLL once again and look at the tlb. See that now it is BSTR instead of LPSTR.

Now run the C++ Console application.


In order to run the mz test application.

  1. Open the C# solution in a VS 2005.
  2. Build the solution.
  3. Use regasm to register the tlb.
  4. Open the C++ dsw in VS 6.
  5. In the #import section, refer to the appropriate location in your machine. "#import "E:\MyTestApps\TestInterOp\Debug\MyInterOp.tlb"".
  6. Run the Console application and that's it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
No Biography provided

Comments and Discussions

QuestionException thrown: read access violation. pTest was nullptr. Pin
Member 1499350715-Nov-20 6:43
MemberMember 1499350715-Nov-20 6:43 
QuestionI had C# DLL making SOAP calls - this requires setting SecurityProtocol Pin
oniDino6-Feb-20 10:47
MemberoniDino6-Feb-20 10:47 
QuestionCan any one explain why LPSTR doesn't work and why we use BStr Pin
Member 1406468516-Aug-19 3:39
MemberMember 1406468516-Aug-19 3:39 
QuestionCalling C# dll form C++ exe Pin
DineshKrish26-Jun-14 0:44
MemberDineshKrish26-Jun-14 0:44 
AnswerRe: Calling C# dll form C++ exe Pin
Daniel Kamisnki18-Sep-18 16:35
MemberDaniel Kamisnki18-Sep-18 16:35 
GeneralRe: Calling C# dll form C++ exe Pin
Member 139774068-Jan-20 9:21
MemberMember 139774068-Jan-20 9:21 
GeneralRe: Calling C# dll form C++ exe Pin
Daniel Kamisnki8-Jan-20 11:20
MemberDaniel Kamisnki8-Jan-20 11:20 
QuestionC++ compilation error (Visual Studio 2010) Pin
charles9228-Aug-13 19:48
Membercharles9228-Aug-13 19:48 
With regard to the C++ code listing above, I'm getting a compilation error (Win 7; VS 2010)
ITest* pTest = NULL;
Error 1 error C2065: 'ITest' : undeclared

I imported the .tlb file, and included the .tli file, which appears to have the declaration of ITest. So I'm baffled as to why the compiler does not know what ITest is. I was hoping a C++ guru could offer suggestions, or perhaps post the complete C++ program showing the import and include statements. (I also tried Visual Studio 6.0, and it did not compile, but for a different reason).

Thanks in advance
QuestionHow about having Action delegate in c# and the action delegate method in c++ how could you code passing the action delgate method to action delegate Pin
a ahole26-Jun-11 17:35
Membera ahole26-Jun-11 17:35 
Generalcan you send a string/struct to the C# dll from C++ Pin
lonojohnson18-May-11 7:07
Memberlonojohnson18-May-11 7:07 
GeneralRe: can you send a string/struct to the C# dll from C++ Pin
Ricky Gai29-Oct-13 23:18
MemberRicky Gai29-Oct-13 23:18 
GeneralFound a not reported bug (with solution) Pin
brian enno13-Jun-10 9:36
Memberbrian enno13-Jun-10 9:36 
GeneralError Message Pin
p0o0q1-Oct-09 23:51
Memberp0o0q1-Oct-09 23:51 
GeneralRe: Error Message [modified] Pin
Stellar Developer4-Jun-10 14:31
MemberStellar Developer4-Jun-10 14:31 
GeneralRe: Error Message Pin
chinki150111-Oct-10 10:12
Memberchinki150111-Oct-10 10:12 
GeneralCalling a C# dll that returns an array of double to C++ Pin
Member 177489114-Feb-07 20:40
MemberMember 177489114-Feb-07 20:40 
Questionthe hr is not created, and everything goes ok, the building, debuging, there is a tlh and tli file ? Pin
petrukon21-Dec-06 5:44
Memberpetrukon21-Dec-06 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.