Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Calling external function in C (http://www.velocityreviews.com/forums/t596919-calling-external-function-in-c.html)

Elstree 03-07-2008 11:34 PM

Calling external function in C
 
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.


Ian Collins 03-07-2008 11:39 PM

Re: Calling external function in C
 
Elstree 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++?
>

The answer lies on a .net group rather than here.

--
Ian Collins.

Richard Heathfield 03-07-2008 11:46 PM

Re: Calling external function in C
 
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Robbie Hatley 03-07-2008 11:52 PM

Re: Calling external function in C
 

"Elstree" 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++?


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

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant



Richard 03-08-2008 06:47 PM

Re: Calling external function in C
 

"Robbie Hatley" <see.my.signature@for.my.email.address> writes:

> "Elstree" 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++?

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

jacob navia 03-08-2008 08:57 PM

Re: Calling external function in C
 
Elstree wrote:
> 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.


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

jacob navia 03-08-2008 09:00 PM

Re: Calling external function in C
 
Richard Heathfield wrote:
> 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.
>


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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

Richard Heathfield 03-08-2008 09:22 PM

Re: Calling external function in C
 
jacob navia said:

<snip>

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

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Joachim Schmitz 03-09-2008 03:27 PM

Re: Calling external function in C
 
Richard Heathfield wrote:
> jacob navia said:
>
> <snip>
>
>> 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.

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

Bye, Jojo



Kenny McCormack 03-09-2008 05:08 PM

Re: Calling external function in C
 
In article <fr0vkl$dul$1@online.de>,
Joachim Schmitz <jojo@schmitz-digital.de> wrote:
>Richard Heathfield wrote:
>> jacob navia said:
>>
>> <snip>
>>
>>> 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.

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



All times are GMT. The time now is 07:36 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.