473,394 Members | 1,739 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,394 software developers and data experts.

c#-c Interoperability

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
Nov 16 '05 #1
2 1495
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/<4E**********************************@microsoft.co m>

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]
Nov 16 '05 #2
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" <Ge****@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: j-integra_support | last post by:
Looking for Java/COM interoperability tools? Thousands of companies world-wide are using J-Integra for COM for interoperability between Java and Microsoft COM applications. J-Integra for COM is a...
4
by: Frederik Kesting | last post by:
Hi! I heard about the possibilty of using different languages for one project whith the .Net Framework. Is it f.e. possible to include Managed C++ code into a VB.Net project without changing...
3
by: sb | last post by:
I think streams are nice, but what do you do when you have to write to or, even worse, read from a FILE*, for example a UNIX stream? C++ streams can not be created from FILE*'s or have them...
1
by: louis | last post by:
Today I got the latest propaganda email from MSFT talking about XML and interoperability. It's been bothering me all day and while the notion of interoperability is good -- the ability to interact...
1
by: Ralph | last post by:
Does anybody know where could i find documentation of .NET interoperability with Office. I know it's through COM objects, but, where could i find documentation of those objects, their methods,...
1
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C#...
3
by: Sai Kit Tong | last post by:
Hi, I am developing a new application running on Windows platform that needs to interface with existing legacy code - written in basic C / C++. I am trying to evaluate Java vs C#...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
0
by: j-integra_support | last post by:
Looking for Java/.NET/CORBA interoperability tools? Intrinsyc Software has just released the latest addition to its popular J-Integra Interoperability Suite... J-Integra Espresso! J-Integra...
0
by: Pradnya Patil | last post by:
Hi , I need to export some of the reports to MS EXCEL & MS WORD in a WEB APPLICATION.I also need to LOCK some of the Columns in EXCEL-sheet.Right now I need to run the Interoperability...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...

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.