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

C call of a C# dll

Hi everybody,

Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?

To be more specific, this is what I have.

1.
C# file Class.cs:

using System; //and some other packages

namespace myns
{
public class Class
{
public Class()
{
}

public static byte[] getByteArray(
string name,
int index,
string format,
ref int n
) {
//Code returning some byte array.
}
}
}

Compiling it, I get myns.dll.

2.
C file mycaller.c:

#include <windows.h>
#include <stdio.h>
#include "mycaller.h"

typedef byte* (*FunctionPointer)(char*, int, char*, int*);

#ifdef __cplusplus
extern "C" {
#endif

byte* myfunc() {

//load the library
HINSTANCE m_libraryHandle = LoadLibrary("myns.dll");
if(m_libraryHandle != NULL) {
printf("Library loaded\n");
}
else {
printf("Library not loaded\n");
return NULL;
}

byte* result = NULL;

//get the function pointer
FunctionPointer functionptr =
(FunctionPointer)GetProcAddress(m_libraryHandle,
"getByteArray");

if(functionptr != NULL) {
printf("Function pointer is OK\n");

int n = -1;
//call the function from the loaded library
result = (*functionptr)("fubar1", 0, "fubar2", &n);

//some code modifying the result
}
else {
printf("Function pointer is NULL\n");
}

//free the library
FreeLibrary(m_libraryHandle);
printf("Library freed\n");

return result;
};

#ifdef __cplusplus
}
#endif

Now, the problem. Library gets loaded but the function pointer is
allways NULL.

1. Should the method call getByteArray() in Class.cs be "externalized"
somehow
in order for the call to be successful? If so, how do I "externalize" it?

2. Is the name of the function in GetProcAddress() wrong? Should I put the
namespace (myns) and class name (Class) in front of the method name? If
so, how
(I mean what delimiters do I use)?

3. Something else?

All help will be greatly appreciated. Thanks.
Nov 16 '05 #1
11 1906
You can register the .NET class as a COM class (see regasm.exe) and use CoCreateInstance and friends to call it - you can create a typelib for your assembly using tlbexp.exe

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi everybody,

Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?

Nov 16 '05 #2
Richard Blewett [DevelopMentor] wrote:
You can register the .NET class as a COM class (see regasm.exe)
and use CoCreateInstance and friends to call it - you can create
a typelib for your assembly using tlbexp.exe


I'm fairly new to .NET and C#. Could you be more specific, please?
Nov 16 '05 #3
Hi
Seems like it should work . check out his article

http://www.c-sharpcorner.com/Code/20...romUnmanagedFB
.asp
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
Nov 16 '05 #4
Richard Blewett [DevelopMentor] wrote:
You can register the .NET class as a COM class (see regasm.exe) and use CoCreateInstance and friends to call it - you can create a typelib for your assembly using tlbexp.exe


Please do not post answers to Microsoft-specific questions to comp.lang.c
dll's, .NET, C# are all Gatesware and have nothing to do with the C
programming language.
Nov 16 '05 #5
Eltee wrote:
Richard Blewett [DevelopMentor] wrote:
You can register the .NET class as a COM class (see regasm.exe)

> and use CoCreateInstance and friends to call it - you can create
> a typelib for your assembly using tlbexp.exe


I'm fairly new to .NET and C#. Could you be more specific, please?


Not in comp.lang.c
Please do not post Microsoft-specific questions to comp.lang.c
dll's, .NET, C# are all Gatesware and have nothing to do with the C
programming language.
Nov 16 '05 #6
Eltee wrote:
Hi everybody,

Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?


Please do not post Microsoft-specific questions to comp.lang.c
dll's, .NET, C# are all Gatesware and have nothing to do with the C
programming language.
Nov 16 '05 #7
Eltee wrote:
Martin Ambuhl wrote:
Eltee wrote:
Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?


Please do not post Microsoft-specific questions to comp.lang.c
dll's, .NET, C# are all Gatesware and have nothing to do with
the C programming language.


But they have. They're written in C/C++. More important, they
have to interact with C and that's what I'm interested in.
Whether you like it or not.


No they don't. There are no such things as dll's in the C
language, nor is their any mention of .net or C#. However if you
quote me chapter and verse from the ISO C standard defining any of
dll, .net, or C# I will retract.

Meanwhile you have peavishly and rudely continued to post off-topic
material on c.l.c, when Martin had plainly set f'ups to confine any
further discussion to an area where it might be on-topic.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 16 '05 #8
Eltee wrote:
infobahn wrote:
Eltee wrote:
Martin Ambuhl wrote:
Eltee wrote:
>
> Is it possible to
> 1. call a function from a dll made with .NET (C#)
> 2. from a program written in plain (as in: not .NET) C or C++?

Please do not post Microsoft-specific questions to comp.lang.c
dll's, .NET, C# are all Gatesware and have nothing to do with the C
programming language.
.... snip ...
More important, they have to interact with C and that's what
I'm interested in. Whether you like it or not.


Your question is more to do with the organisation of Windows DLLs
than it is to do with the C programming language, so it may be a
better use of your time to ask the folks in a Windows newsgroup,
such as comp.os.ms-windows.programmer.win32. Whether you like it
or not. :-)


Ooh, I like it. Do you know you're the only person in here who gave me
some useful advice? I thank you from the bottom of my heart.


If you make a large effort and exercize your miniscule brainlet,
you will see that Martin and then Infobahn gave you precise reasons
for not posting your query on c.l.c. However you insist on
shouldering your way in here with foolish and annoying comments.
This is a very poor reflection on the training you received from
your parents, or possibly indicates you are still too young to have
received such a benefit at all. If you happen to be "mentally
challenged" at least say so, and allow others to then give you the
benefit of the doubt.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 16 '05 #9
Eltee <el***@hotmail.com> spoke thus:
Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?


These links should put the responses you have received thus far in
context.

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 16 '05 #10
Eltee wrote:

All help will be greatly appreciated. Thanks.


I recommend that you ask this question in a C# newsgroup.
You'll probably have a better reception there.
The reason is simply because there are programmers here
that aren't programming for Microsoft Windows.
( FWIW, I really wish they had C platform specific newsgroups. )
Nov 16 '05 #11
"Richard Blewett [DevelopMentor]" wrote:

You can register the .NET class as a COM class (see regasm.exe)
and use CoCreateInstance and friends to call it - you can create
a typelib for your assembly using tlbexp.exe .... snip ...
Is it possible to
1. call a function from a dll made with .NET (C#)
2. from a program written in plain (as in: not .NET) C or C++?


This is way off-topic for c.l.c. Portable C code knows nothing
about classes, typelibs, dlls, .NET, etc. c.l.c deals only with
portable code that can execute on any system with a standards
compliant compiler system. F'ups set.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 16 '05 #12

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

Similar topics

23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
35
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
5
by: Amaryllis | last post by:
I'm trying to call a CL which is located on our AS400 from a Windows application. I've tried to code it in different ways, but I seem to get the same error every time. Does anyone have any clue...
13
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.