473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Calling external functions

I am at my wits end here. I have a simple project with one global function
returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how in
the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.
Dec 8 '05 #1
6 6085
Tim,

You should have received an error when you added it to your project. If
you have a function that is exported by a DLL, you need to call it through
the P/Invoke layer. If the dll is somewhere in the path where LoadLibrary
will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global function
returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how in
the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.

Dec 8 '05 #2
I did that exactly and it says "Unable to find an entry point named gettime
in DLL ctime.dll"
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uz**************@TK2MSFTNGP09.phx.gbl...
Tim,

You should have received an error when you added it to your project.
If you have a function that is exported by a DLL, you need to call it
through the P/Invoke layer. If the dll is somewhere in the path where
LoadLibrary will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global
function returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how in
the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.


Dec 8 '05 #3
this should be so simple....here is the code in my simple "C" dll

#define DllExport __declspec(dllexport)

#include <time.h>

//DllExport

long gettime(void);

long gettime()

{

time_t tt = time(NULL);

return (long)tt;

}

--------------------------------------------------------------------------
here is the import in my C# app

[DllImport("ctime.dll", CallingConvention=CallingConvention.Cdecl)]

public static extern long gettime();

-------------------------------------------

I've tried several different CallingConventions and nothing same thing
"Unable to find entry point named gettime in dll ctime.dll


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uz**************@TK2MSFTNGP09.phx.gbl...
Tim,

You should have received an error when you added it to your project.
If you have a function that is exported by a DLL, you need to call it
through the P/Invoke layer. If the dll is somewhere in the path where
LoadLibrary will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global
function returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how in
the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.


Dec 9 '05 #4
The line
//DllExport

was not always commented ....
"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:uq**************@TK2MSFTNGP14.phx.gbl...
this should be so simple....here is the code in my simple "C" dll

#define DllExport __declspec(dllexport)

#include <time.h>

//DllExport

long gettime(void);

long gettime()

{

time_t tt = time(NULL);

return (long)tt;

}

--------------------------------------------------------------------------
here is the import in my C# app

[DllImport("ctime.dll", CallingConvention=CallingConvention.Cdecl)]

public static extern long gettime();

-------------------------------------------

I've tried several different CallingConventions and nothing same thing
"Unable to find entry point named gettime in dll ctime.dll


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uz**************@TK2MSFTNGP09.phx.gbl...
Tim,

You should have received an error when you added it to your project.
If you have a function that is exported by a DLL, you need to call it
through the P/Invoke layer. If the dll is somewhere in the path where
LoadLibrary will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global
function returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how
in the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.



Dec 9 '05 #5
AAAHHHHH changed the name of my code file from .cpp to .c removed the
function prototype and now it finds it....go figure


"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
The line
//DllExport

was not always commented ....
"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:uq**************@TK2MSFTNGP14.phx.gbl...
this should be so simple....here is the code in my simple "C" dll

#define DllExport __declspec(dllexport)

#include <time.h>

//DllExport

long gettime(void);

long gettime()

{

time_t tt = time(NULL);

return (long)tt;

}

--------------------------------------------------------------------------
here is the import in my C# app

[DllImport("ctime.dll", CallingConvention=CallingConvention.Cdecl)]

public static extern long gettime();

-------------------------------------------

I've tried several different CallingConventions and nothing same thing
"Unable to find entry point named gettime in dll ctime.dll


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uz**************@TK2MSFTNGP09.phx.gbl...
Tim,

You should have received an error when you added it to your project.
If you have a function that is exported by a DLL, you need to call it
through the P/Invoke layer. If the dll is somewhere in the path where
LoadLibrary will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global
function returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how
in the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.



Dec 9 '05 #6
In C++ you have to export your functions with "C linkage", that is, you need
to wrap your functions or prototypes in a C linkage block.

extern "C"
{
__declspec(dllexport) long gettime();
...
}

long gettime()
{
...
}
Note also that your function declaration in C# has the wrong return type, a
long in C/C++ is 32 bit a long in C# is 64 bit.

[C#]
public static extern int gettime();
Willy.

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:uq**************@TK2MSFTNGP14.phx.gbl...
this should be so simple....here is the code in my simple "C" dll

#define DllExport __declspec(dllexport)

#include <time.h>

//DllExport

long gettime(void);

long gettime()

{

time_t tt = time(NULL);

return (long)tt;

}

--------------------------------------------------------------------------
here is the import in my C# app

[DllImport("ctime.dll", CallingConvention=CallingConvention.Cdecl)]

public static extern long gettime();

-------------------------------------------

I've tried several different CallingConventions and nothing same thing
"Unable to find entry point named gettime in dll ctime.dll


"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:uz**************@TK2MSFTNGP09.phx.gbl...
Tim,

You should have received an error when you added it to your project.
If you have a function that is exported by a DLL, you need to call it
through the P/Invoke layer. If the dll is somewhere in the path where
LoadLibrary will find it, you can do this:

[DllImport("<dll name here>.dll")]
static extern int myfunc();

And then call the dll function in managed code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim Greenwood" <tim_greenwood A-T yahoo D-O-T com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I am at my wits end here. I have a simple project with one global
function returning a long value.

__declspec(dllexport) long myfunc()
{
}

I've built the dll, added it as a reference to my C# project. Now how
in the world do I reference "myfunc" ????
It totally eludes me.

Many thanks for any help here.



Dec 9 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Kyler Laird | last post by:
I need to submit C/C++ code for a class. (It's not a programming class. The choice of language is inertial. I think that it mostly serves to distract students from the course subject.) I'm...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
11
by: j23 | last post by:
I have library (static) testlib.cpp: #include <stdarg.h> void xxx(...) { char buf; va_list args; va_start(args, buf); va_end(args); }
3
by: Gustavo L. Fabro | last post by:
Greetings! I'm a newbie in Visual C++ .NET (have programmed in Borland C++ and Builder for long) and I am trying to do a very simple thing, but I'm stuck. I created an (unmanaged) DLL project...
12
by: Ron | last post by:
Greetings, I am trying to understand the rational for Raising Events instead of just calling a sub. Could someone explain the difference between the following 2 scenarios? Why would I want to...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Maydogg6 | last post by:
I need a hand with some stubborn link errors. I'm trying to recreate and old program from 6.0 into .NET, but for some reason when I try to compile I'm getting linking errors for all my function...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.