Calling external function in C

E

Elstree

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

Ian Collins

Elstree 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++?
The answer lies on a .net group rather than here.
 
R

Richard Heathfield

Elstree said:
Hi everybody,

Is it possible to
1. call a function from a dll made with .NET (C#)

Try asking Q1 in microsoft.public.dotnet.languages.csharp
2. from a program written in plain (as in: not .NET) C or C++?

Yes, this is certainly possible. The way I do it is to write the DLL code
in standard C, construct a .def file that describes all the symbols I want
to export (that's function names, basically), and tell the implementation
to compile it as a DLL. That should produce an import library as well as
the DLL itself, and I simply link the import library into the program that
calls the DLL, just as if it were a normal static library. This is all
very easy, and doesn't require Win32-pollution of the source base.
 
R

Robbie Hatley

Elstree said:
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++?

Depends on your OS, compiler, etc. Not a C-language matter
and hence not on-topic in this group.

Read the manuals, and also ask in these groups:

microsoft.public.dotnet.languages.csharp
microsoft.public.dotnet.languages.vc
microsoft.public.win32.programmer
comp.os.ms-windows.programmer.win32
comp.os.ms-windows.programmer.misc
 
R

Richard

Robbie Hatley said:
Depends on your OS, compiler, etc. Not a C-language matter
and hence not on-topic in this group.

Read the manuals, and also ask in these groups:

microsoft.public.dotnet.languages.csharp
microsoft.public.dotnet.languages.vc
microsoft.public.win32.programmer
comp.os.ms-windows.programmer.win32
comp.os.ms-windows.programmer.misc

Out of curiosity, on average, how many times a day do you post OT replies?
 
J

jacob navia

Elstree said:
Hi everybody,

Is it possible to
1. call a function from a dll made with .NET (C#)

Yes, using the COM interface
2. from a program written in plain (as in: not .NET) C or C++?

Yes, lcc-win does this.

To be more specific, this is what I have.

[snip]

That can't work. You have to create an object, and use COM
to talk with it. This can be done automatically, and we have
developed an interface that does this.

If you want to buy it please contact
q-software-solutions.com, or contact me by private mail.

If not, you can always developed it yourself. It is not that
complicated if you know COM.
 
J

jacob navia

Richard said:
Elstree said:


Try asking Q1 in microsoft.public.dotnet.languages.csharp


Yes, this is certainly possible. The way I do it is to write the DLL code
in standard C, construct a .def file that describes all the symbols I want
to export (that's function names, basically), and tell the implementation
to compile it as a DLL. That should produce an import library as well as
the DLL itself, and I simply link the import library into the program that
calls the DLL, just as if it were a normal static library. This is all
very easy, and doesn't require Win32-pollution of the source base.

You misunderstood the question. He wants to link with a dll built
with .NET.

He doesn't want to build a dll or link with a dll using C
 
R

Richard Heathfield

jacob navia said:

You misunderstood the question. He wants to link with a dll built
with .NET.

He doesn't want to build a dll or link with a dll using C

I must admit I never thought I'd say this, but - yes, you're right.

I've made a note on the calendar.
 
J

Joachim Schmitz

Richard said:
jacob navia said:



I must admit I never thought I'd say this, but - yes, you're right.

I've made a note on the calendar.
Was that remark realy neccessary? A simple "yes, you're right" would have
done.

Bye, Jojo
 
K

Kenny McCormack

Was that remark realy neccessary? A simple "yes, you're right" would have
done.

Yes, a simple "yes, you're right" would have been minimally standards
compliant, but would not have been in the spirit of this NG. We expect
better from our warriors.

Surely, even you know tht...
 

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

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top