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

Eltee

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

Mark McIntyre

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

infobahn

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

Eltee

Mark said:
a) there's no such language as C/C++,

You're right there. Absolutely right. Then again, there's no such
language as C either. There's ANSI C, there's this C, there's that C.
And there's another C. BTW: which flavor is allowed to be discussed in
this NG?

Almost the same with C++. But I'm sure you knew that.
> 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.

Right, silly me. BTW: are you guys always so helpful or are you just
trying to be elitistic?
Who cares what you are interested in?

I thought that maybe the ones who are not interested would just ignore
my post. And the ones who reply would give me at least some directions
other than "go away". But heck, that's life. I'm not complaining.
This is comp.lang.c, not
comp.lang.eltees.interests.




The point is, the topic here is the C language,

Yeah, right. Which C language, again?
> not how some
vendor-specific extension of it, with its own newsgroup for crissake,

Which NG would that be, BTW? If I go into some M$ NG and mention MinGW
they'll do what you do: try to scare me off.
interacts with some other random language.

Other than some other flavor of C you mean?
 
E

Eltee

CBFalconer said:
No they don't. There are no such things as dll's in the C
language,

Right, silly me. I should've said so's, right. How 'bout those?
> nor is their any mention of .net or C#.

Neither is there any mention of English. And yet, English comes in handy
in this ta-da! _C_ NG.
> However if you
quote me chapter and verse from the ISO C standard

Oh, so that's the flavor. Makes me wonder, though, why this isn't
comp.lang.ISO.c.
> defining any of
dll, .net, or C# I will retract.

Don't worry, you won't have to.
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.

Right, a great sin, a great sin. I'll have to mention it next sunday.
 
E

Eltee

infobahn said:
I presume you mean that they are written in a mixture of C and C++.

Right. Finally some common sense.
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.)




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

Eltee

CBFalconer said:
... snip ...



If you make a large effort and exercize your miniscule brainlet,

Minuscule brainlet? Wow, that's pretty refreshing after all the nice
help I received in this NG.
you will see that Martin and then Infobahn gave you precise reasons
for not posting your query on c.l.c.

What precise reasons? Martin said that Gatesware had nothing to do with
C. Appart from that #include <windows.h> and (god forbid!) a dll
mentioned, the second code snippet I posted had nothin to do with Gates
and was, in fact, writen in C. If you guys are so geekishly elitistic
that it's beyond your standard to say "look, I think you'd find better
answers in comp.<whatever> than here", then you crowns-of-creation
should stick to the moderated groups (which do exist, btw) or even
closed mailing lists, and orgasm over "pure" C in private.
> However you insist on
shouldering your way in here

Where _is_ here? Shoulder? I think this is a public NG. I don't need to
shoulder my way in.
> with foolish and annoying comments.
This is a very poor reflection on the training you received from
your parents,

Unlike you perhaps, I wasn't trained. I don't know about your hometown,
but where I live people generaly don't train their children. They train
their dogs.
> 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.

All this hate just as a reaction to a (alleged) mispost? Sheesh! I
really must keep out.
 
I

infobahn

Eltee said:
All this hate just as a reaction to a (alleged) mispost? Sheesh! I
really must keep out.

That would be a shame. I don't wish to become embroiled in a
flamefest, but I hope you'll appreciate that C is such a
popular language that those who wish to discuss the language
itself, as opposed to the thousands of libraries and programs
written in (and for) it, find it necessary to guard the
topicality of this newsgroup very closely. If they didn't,
the newsgroup would quickly descend into an abyss of
platform-specific questions that mean little to the majority
of the readers of the group, at which point those C experts
that make this newsgroup most useful would in all likelihood
stop bothering to read it at all. Such an outcome would be
very regrettable, but inevitable. More or less the same
thing happened to comp.lang.c++ a few years ago, and it took
them a long time to recover.

There are, of course, lots of newsgroups dealing with questions
about C#, about Windows, about DLLs, and so on and so forth, so
it does make sense to direct such questions to those groups.

It is a little unfortunate that some longstanding comp.lang.c
regulars consider it necessary (or, perhaps, fun?) to lambast
those who innocently cross the topicality line, using pejorative
language that could easily be avoided. Nevertheless, it is
hardly surprising that their patience is tried from time
to time, given the number of times they have to explain the
topicality rules to newcomers.

This newsgroup is a wonderful learning resource for the C
language itself, as defined by ISO/IEC 9899 (the 1990 and
1999 flavours) or - for historical reasons - by K&R's
original book. I hope you will feel welcome here when you
have questions about the core language itself, and have the
good sense to seek platform-specific newsgroups when you
have platform-specific questions.

As I mentioned before, comp.os.ms-windows.programmer.win32
is an excellent "catch-all" Windows programming newsgroup,
whose experienced contributors should either be able to
answer your question or point you to an even more specialised
newsgroup.

Good luck in your search for an answer. Take care.
 
C

CBFalconer

infobahn said:
.... snip ...

It is a little unfortunate that some longstanding comp.lang.c
regulars consider it necessary (or, perhaps, fun?) to lambast
those who innocently cross the topicality line, using pejorative
language that could easily be avoided. Nevertheless, it is
hardly surprising that their patience is tried from time
to time, given the number of times they have to explain the
topicality rules to newcomers.

The thing that was lambasted was the rude and ignorant insistance
on butting back in with OT drivel, especially as a response to
something that had follow-ups set precisely to avoid said drivel.
Who was it said "it does it only to annoy" in Alice?
 
K

Keith Thompson

Eltee said:
You're right there. Absolutely right. Then again, there's no such
language as C either. There's ANSI C, there's this C, there's that
C. And there's another C. BTW: which flavor is allowed to be discussed
in this NG?

Yes, there is such a language as C. There are also a number of C-like
dialects. We discuss the former, not the latter.

We discuss C as defined by the 1990 and 1999 ISO standards, and
sometimes historical versions of C a defined by K&R1 and earlier
documents. We find that these restrictions don't create any lack of
things to talk about, as you can tell from the high volume of this
newsgroup.

[snip]
Right, silly me. BTW: are you guys always so helpful or are you just
trying to be elitistic?

If someone has a question about standard C, we tend to be very
helpful. For questions about anything else, the best help we can give
is to advise the questioner to ask it somewhere else. We're not
trying to be elitist; we're trying to maintain the focus of this
newsgroup, and we're acknowledging the limits of our expertise.

Ignoring off-topic posts doesn't work. First, it would require
unanimous agreement to ignore them, which is impractical. Second,
they would quickly drown the newsgroup; this has happened in other
groups, and we don't want it to happen here.
 
M

Mark McIntyre

You're right there. Absolutely right. Then again, there's no such
language as C either. There's ANSI C, there's this C, there's that C.
And there's another C.

There's exacly one computer langauge called C. Its defined by the ISO
standard.
BTW: which flavor is allowed to be discussed in this NG?

Guess.
Right, silly me. BTW: are you guys always so helpful or are you just
trying to be elitistic?

We're always so helpful. It only looks elitist if you're too stupid to
realise you're asking about buying socks in a greengrocers.
I thought that maybe the ones who are not interested would just ignore
my post. And the ones who reply would give me at least some directions
other than "go away". But heck, that's life. I'm not complaining.

Round here, we agressively defend teh topic of the group, because otherwise
it gets filled with people asking offtopic quesstions, and idiots answering
them with incorrect, misleading or plain bollocks answers.
Which NG would that be, BTW?

I have no clue. Get a newsreader with search facilities.
Other than some other flavor of C you mean?

C has no flavours. And there's a U in flavour.
 
M

Mark McIntyre

What precise reasons? Martin said that Gatesware had nothing to do with
C.

Yes. And he's right.
Appart from that #include <windows.h> and (god forbid!) a dll
mentioned, the second code snippet I posted had nothin to do with Gates

windows.h is the Windows platform header. If thats not Windows specific,
nothing is.
and was, in fact, writen in C. If you guys are so geekishly elitistic
that it's beyond your standard to say "look, I think you'd find better
answers in comp.<whatever> than here",

What better answer is there, than to point you at the experts? We're not
windows experts, why ask us?
then you crowns-of-creation
should stick to the moderated groups (which do exist, btw) or even
closed mailing lists, and orgasm over "pure" C in private.

CLC was here first. We like this group like it is. If you want to discuss
someething else, go elsewhere.

Unlike you perhaps, I wasn't trained. I don't know about your hometown,
but where I live people generaly don't train their children.

This explains /a lot/ about some of the posters here. Of course people
train their kids - you train them to say please and thankyou, not to hit
other kids, not to sh*t on granny's carpet, not to steal ferraris.
All this hate just as a reaction to a (alleged) mispost? Sheesh! I
really must keep out.

No don't do that. But do post topical posts.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top