Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C call of a C# dll

Reply
Thread Tools

C call of a C# dll

 
 
Eltee
Guest
Posts: n/a
 
      12-19-2004
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.
 
Reply With Quote
 
 
 
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      12-19-2004
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++?

 
Reply With Quote
 
 
 
 
Eltee
Guest
Posts: n/a
 
      12-19-2004
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?
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-19-2004
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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-19-2004
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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-19-2004
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.
 
Reply With Quote
 
Eltee
Guest
Posts: n/a
 
      12-19-2004
Martin Ambuhl wrote:
> 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.


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.
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-19-2004
On Sun, 19 Dec 2004 22:18:19 +0100, in comp.lang.c , Eltee
<(E-Mail Removed)> wrote:

>Martin Ambuhl wrote:
>> 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.

>
>But they have. They're written in C/C++.


a) there's no such language as C/C++, you surely know that C and C++ are
different (and incompatible) languages

b) .NET and C# are NOT written in C or C++, they're written using
Microsoft's extensions to the C++ language which are not part of C++ and
certainly ain't part of C.

>More important, they have to interact with C and that's what I'm interested in.


Who cares what you are interested in? This is comp.lang.c, not
comp.lang.eltees.interests.

>Whether you like it or not.


The point is, the topic here is the C language, not how some
vendor-specific extension of it, with its own newsgroup for crissake,
interacts with some other random language.




--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-19-2004
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 ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
infobahn
Guest
Posts: n/a
 
      12-19-2004
Eltee wrote:

> Martin Ambuhl wrote:
>
>> 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.

>
>
> But they have. They're written in C/C++.


I presume you mean that they are written in a mixture of C and C++.
That in itself is not sufficient to make them relevant in this
newsgroup, I'm afraid. (If it were, the newsgroup would become
too general to be useful.)

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Second long argument to a JNI call on a Win32 DLL contains zero when it reaches the DLL pcarr01 Java 3 02-17-2005 09:30 AM
How to determine if a DLL is a COM DLL or .NET DLL Anushi ASP .Net 5 10-28-2004 01:59 PM
Why does Ruby use both tcl83.dll and tk83.dll (instead of just tk83.dll)? H. Simpson Ruby 4 08-03-2004 04:45 PM
mprapi.dll --> samlib.dll --> ntdll.dll issue. Some1 Computer Support 4 04-05-2004 02:02 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments