473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing C++ functions in C#

A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C and
C++ that I am trying to access via C#. Is there an easy way to do this? If
not, should I just rewrite the SDK in C#?

Thanks.

James
Nov 17 '05 #1
6 1407
"James Crouch" <ja**********@wpafb.af.mil> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C and C++ that I am trying to access via C#. Is there an easy way to do this? If not, should I just rewrite the SDK in C#?


There are undoubtedly some horrid ways to access C++ directly from C#. The
best way though would be to write a managed C++ wrapper for your SDK. It
would be very quick and painless.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
Hi,

depend of how it's implemented,
if it's a COM component you can access it very easily.

if it's a native dll, you can use P/Invoke with this you can get in trouble
dealing with memory allocation, converting data from managed to unmanaged
memory , etc , how easy ( or how ) complicated depend of the library itself.

of course, the idea of rewrite all in c# is valid too :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"James Crouch" <ja**********@wpafb.af.mil> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C
and C++ that I am trying to access via C#. Is there an easy way to do
this? If not, should I just rewrite the SDK in C#?

Thanks.

James

Nov 17 '05 #3
Tim,

Where would I find instructions on how to write a managed C++ wrapper?

James

"Tim Haughton" <ti*********@gmail.com> wrote in message
news:Jq*******************@fe08.news.easynews.com. ..
"James Crouch" <ja**********@wpafb.af.mil> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
A couple of questions that probably has been asked before but I can't
find
anything on the newsgroup. I have a communication SDK file written in C

and
C++ that I am trying to access via C#. Is there an easy way to do this?

If
not, should I just rewrite the SDK in C#?


There are undoubtedly some horrid ways to access C++ directly from C#. The
best way though would be to write a managed C++ wrapper for your SDK. It
would be very quick and painless.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Nov 17 '05 #4

"James Crouch" <ja**********@wpafb.af.mil> wrote in message
news:u3*************@tk2msftngp13.phx.gbl...
A couple of questions that probably has been asked before but I can't find
anything on the newsgroup. I have a communication SDK file written in C
and C++ that I am trying to access via C#. Is there an easy way to do
this? If not, should I just rewrite the SDK in C#?

Thanks.

James


It depends, C# can call exported "C functions", that is functions with "C
linkage" exported from a native DLL can be called through the PInvoke
interop layer (see MSDN for details, search PInvoke and DllImport).
C++ class members however cannot be accesses directly, however unmanaged
classes can be wrapped by a managed equivalent and as such be consumed by
C#.
Consult MSDN for detailed info on both PInvoke and native interop at:
http://msdn2.microsoft.com/en-us/library/ms140906

Willy.
Nov 17 '05 #5
You may be able to use COM interop
http://www.geocities.com/Jeff_Louie/...op/atl_com.htm

Or use PInvoke
http://www.geocities.com/Jeff_Louie/...op/pinvoke.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
"James Crouch" <ja**********@wpafb.af.mil> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Tim,

Where would I find instructions on how to write a managed C++ wrapper?

James


Start here:

http://msdn.microsoft.com/library/de...art1_Start.asp

It's a lot easier than writing a COM wrapper IMHO. But then I've never been
a COM fan. Any problems, post back. This is something I've done quite a bit
and it's a well trodden path.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #7

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

Similar topics

4
3381
by: keepyourstupidspam | last post by:
Hello, I have a class X with a member function setConfigValues(). I want to access this member function in another class Y. Y is not inherited from X. A member function of class X called run()...
6
2588
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" <<...
2
1642
by: Stu | last post by:
Hi, I am migrating a large app to .Net 2 and am having problems with accessing hidden fields using javascript. As I am using masterpages the control names are prefixed with the control...
12
11672
by: Steve Blinkhorn | last post by:
Does anyone know of a way of accessing and modifying variables declared static within a function from outside that function? Please no homilies on why it's bad practice: the context is very...
6
1266
by: Ant | last post by:
I have the following code which works fine for running some tests defined within a module: def a_test(): print "Test A" def b_test(): print "Test B" if __name__ == "__main__":
1
1357
by: DelphiLover | last post by:
Hi. I'm reading and reading, testing and testing, trying to figure out how to do things, how to do things according to best practises and how to do things in the best object oriented way. ...
4
8482
by: Joseph Paterson | last post by:
Hi all, I'm having some trouble with the following code (simplified to show the problem) class Counter { protected: int m_counter; }
9
1937
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
6
8131
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
16
6757
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
7105
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
6967
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
7341
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
5439
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,...
1
4870
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...
0
4564
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...
0
3076
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...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
266
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...

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.