c#-c Interoperability

G

Guest

hi friends,

This is really urgent, Pls.

Can C access C# classes?

I am doing an interop with a C# library and a C library.

The code for the C# library is like this


namespace LibraryNamespace
{
using System;
..........


[Guid("32511C6F-B0AB-4cc9-92E8-2D1FDDDD012C")]
public interface IManagedInterface
{ string getValue(string name);}

[Guid("CD034B6C-FB86-445e-822B-E70BEEEE6995")]
public class LibraryImplementation : IManagedInterface {
public string getValue(string name) {
Library.WebService.OService myService = new Library.WebService.OService();
string str;
StringBuilder sb = new StringBuilder();
StringBuilder spce = new StringBuilder("=");
DataSet ds = new DataSet();
str = myService.getName2(name,opt,sze);
TextWriter tw = new StreamWriter("XMLString.xml");
tw.WriteLine(str.ToString());
tw.Close();

ds.ReadXml("XMLString.xml");
foreach (DataRow dr in ds.Tables[0].Rows)
{
sb=sb.Append(dr["XYZ"];
}
return (Convert.ToString(sb)); }}}

Now, instead of returning a string to the C code, I want to return a
structure so that I could directly map the results to the structure at the C
code instead of parsing and converting the strings.

(1) Where should I declare the structure in the C# code and how, pls give
example
(2) How can I access that in the C code and will it understand the C#
structure.


Pls Help.
Thanks
 
R

Richard Blewett [DevelopMentor]

create a public struct

[ LayouKind(LayoutKind.Sequential)]

public struct Person

{

public int age;

[MarshalAs(UnmanagedType.Bstr)]

public string name;

}

This will be equivelent to the C structure

struct Person

{

int age

BSTR name;

}

The LayouKind attribute tells the runtime to lay the object out in memory in the order that it is declared (the runtime will normally layout a type to make it most efficient to process)

The MarshalAs attribute tells the interop layer to turn this string into a BSTR as strings have a number of representations in the unmanaged world.

You'll need to run REGASM.EXE on the generated assembly to create the COM registry entries, then you can create an instance of the .NET object using CoCreateInstance

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

hi friends,

This is really urgent, Pls.

Can C access C# classes?

I am doing an interop with a C# library and a C library.

The code for the C# library is like this


namespace LibraryNamespace
{
using System;
.........


[Guid("32511C6F-B0AB-4cc9-92E8-2D1FDDDD012C")]
public interface IManagedInterface
{ string getValue(string name);}

[Guid("CD034B6C-FB86-445e-822B-E70BEEEE6995")]
public class LibraryImplementation : IManagedInterface {
public string getValue(string name) {
Library.WebService.OService myService = new Library.WebService.OService();
string str;
StringBuilder sb = new StringBuilder();
StringBuilder spce = new StringBuilder("=");
DataSet ds = new DataSet();
str = myService.getName2(name,opt,sze);
TextWriter tw = new StreamWriter("XMLString.xml");
tw.WriteLine(str.ToString());
tw.Close();

ds.ReadXml("XMLString.xml");
foreach (DataRow dr in ds.Tables[0].Rows)
{
sb=sb.Append(dr["XYZ"];
}
return (Convert.ToString(sb)); }}}

Now, instead of returning a string to the C code, I want to return a
structure so that I could directly map the results to the structure at the C
code instead of parsing and converting the strings.

(1) Where should I declare the structure in the C# code and how, pls give
example
(2) How can I access that in the C code and will it understand the C#
structure.


Pls Help.
Thanks

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.760 / Virus Database: 509 - Release Date: 10/09/2004



[microsoft.public.dotnet.languages.csharp]
 
W

Willy Denoyette [MVP]

Here's a small sample consisting of a cs and a cpp file.
Compile directives given in each individual file.

// Compile with: csc /t:library inprocforcpp.cs
// Register with: Regasm /codebase /tlb inprocforcpp.dll
//
using System.Runtime.InteropServices;
using System;

[StructLayout(LayoutKind.Sequential)]
public struct Customer
{
public int number;
[MarshalAs(UnmanagedType.BStr)]
public string name;
}

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("49414ff5-3820-4ed3-a2e5-95687d1ba892")]
public interface ICustomer
{
Customer GetCustomer(int number);
}

// class
[ClassInterface(ClassInterfaceType.None)]
[Guid("fa7e5cdd-766f-4541-9b9b-aee0cdde5c74")]
[ProgId("Test.Customer")]
public class DotNetInterface : ICustomer
{
public DotNetInterface() {}
[ComVisible(true)]
public Customer GetCustomer(int number)
{
Customer cust = new Customer();
cust.number = number;
cust.name = "My best customer";
return cust;
}
}



///////////////////////////////////
// C++ program
///////////////////////////////////
// Compile with : cl /EHsc inprocclient.cpp
#define _WIN32_WINNT 0x0501
#include <objbase.h>
#include <iostream>
#import "inprocforcpp.tlb" no_namespace named_guids

int main ()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
ICustomerPtr pPtr(__uuidof(DotNetInterface));
int num = 1000;
Customer cust = pPtr->GetCustomer(num);
printf("%d '%S'\n",cust.number, cust.name);
}


Willy.


GeRmIc said:
hi friends,

This is really urgent, Pls.

Can C access C# classes?

I am doing an interop with a C# library and a C library.

The code for the C# library is like this


namespace LibraryNamespace
{
using System;
.........


[Guid("32511C6F-B0AB-4cc9-92E8-2D1FDDDD012C")]
public interface IManagedInterface
{ string getValue(string name);}

[Guid("CD034B6C-FB86-445e-822B-E70BEEEE6995")]
public class LibraryImplementation : IManagedInterface {
public string getValue(string name) {
Library.WebService.OService myService = new Library.WebService.OService();
string str;
StringBuilder sb = new StringBuilder();
StringBuilder spce = new StringBuilder("=");
DataSet ds = new DataSet();
str = myService.getName2(name,opt,sze);
TextWriter tw = new StreamWriter("XMLString.xml");
tw.WriteLine(str.ToString());
tw.Close();

ds.ReadXml("XMLString.xml");
foreach (DataRow dr in ds.Tables[0].Rows)
{
sb=sb.Append(dr["XYZ"];
}
return (Convert.ToString(sb)); }}}

Now, instead of returning a string to the C code, I want to return a
structure so that I could directly map the results to the structure at the
C
code instead of parsing and converting the strings.

(1) Where should I declare the structure in the C# code and how, pls give
example
(2) How can I access that in the C code and will it understand the C#
structure.


Pls Help.
Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top