C call of a C# dll

E

Eltee

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.
 
R

Richard Blewett [DevelopMentor]

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++?
 
E

Eltee

Richard said:
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?
 
M

Martin Ambuhl

Richard said:
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.
 
M

Martin Ambuhl

Eltee said:
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.
 
M

Martin Ambuhl

Eltee said:
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.
 
C

CBFalconer

Eltee said:
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.
 
C

CBFalconer

Eltee said:
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.
 
M

Michael Anonymous

Eltee said:
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. )
 
C

CBFalconer

Richard Blewett said:
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.
 

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