News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

how is that done?

Started by rkorko, August 13, 2012, 11:56:04 PM

Previous topic - Next topic

rkorko

hi,

i hope someone can explain how that would be done. i want to wrap a call to int _cprintf(const char * restrict format, ...); in to a dll that calls it.

ex.:

_declspec(dllexport) int __stdcall conprint(const char * restrict format, ...) {
return _cprintf(format, ...);  <------ ?????????? how do i call this function???
}


any help would be greatly appreciated.

thanks

henry

CommonTater

#1
In MyDLL.c ... 

_declspec(dllexport) int _stdcall function(...

In MyDLL.h...

#pragma lib "MyDLL.lib"  // autocreated library header for mydll

_declspec(dllimport) int _stdcall function(...


In your code...

#include "MyDLL.h"

x = function(...

rkorko

thanx for you reply.
but what i ment, was how does the call inside the function look like.

return _cprintf(format, ...);  <------ ?????????? how do i call this function???

the function header gives
_declspec(dllexport) int __stdcall conprint(const char * restrict format, ...) {

and how would i call return _cprintf(format, ...);  <------ ?????????? how do i call this function??? inside the function?
maybe you can give me another helpful hint.

henry

CommonTater

#3
The inner one it's just a normal function call that is linked into the DLL when you compile it, just like in an EXE.

Where you might be having a problem is that you have a function with a variable number of parameters inside a function with a variable number of parameters...  I've never had to do that one, so maybe one of the others can offer a bit of advice on how to do it.

rkorko

yes, but how do i call it.
i don't know the parmeters that are given to _declspec(dllexport) int __stdcall conprint(const char * restrict format, ...).

ex.: conprint("%s %d %s %s", a,b,c,d). how do i give the paramets to return _cprintf(format, ...)?

CommonTater

We were writing at the same time...

As I said, I've never had to do that one, so maybe one of the others can help...

rkorko

thank you very much anyway. i hope someone can help me.

CommonTater

#7
Two thoughts...

1) Just us _cprintf() directly without the DLL wrapper.

2) You can use the standard printf() with no muss or fuss. 

Is there some reason to write such a thinly wrapped function?

rkorko

Quote
1) Just us _cprintf() directly without the DLL wrapper.
2) You can use the standard printf() with no muss or fuss.
Is there some reason to write such a thinly wrapped function?
unfortunately i need to export this function to another compiler/language and i need the console-output, that the other compiler doesn't have. the library (dll) is a cbtree/isam that i want to test with that compiler. testing it with pelles c has certain problems, such as somefunction(char *x, char *y). on return, x and y are not modified as they should, they retain the old values.


aardvajk

You can't do that. C doesn't allow it. You can use vcprintf though, which lets you forward the arguments using a va_list.


#include <stdio.h>
#include <stdarg.h>

int __cdecl conprint(const char *fmt, ...)
{
    va_list args;
    int ret = 0;

    va_start(args, fmt);
    ret = _vcprintf(fmt, args);
    va_end(args);
    return ret;
}


Note that your function has to be __cdecl instead of stdcall because your code doesn't know how many arguments its going to be called with (the number and size of arguments is required for stdcall)

CommonTater

Quote from: rkorko on August 14, 2012, 10:10:40 AM
testing it with pelles c has certain problems, such as somefunction(char *x, char *y). on return, x and y are not modified as they should, they retain the old values.

I can see where it might be a problem in your case, but generally this is a good thing... In C variables have a "scope" marked by { } in which you can affect them, outside of their scope variables with the same names are not affected.  For example:

#include <stdio.h>

int func(int x)
  {
   x += 3;
   printf("%d\n",x);  //prints 13
   return x;
  }

int main (void)
  {
   int x = 10;
   func(x); 
   printf("%d\n",x);  //prints 10
   x = func(x);
   printf("%d\n",x); // prints 13
   return 0;
  }

Passing in pointers such as *x will let you modify the variable in the calling scope, but not the pointer. For example...

#include <stdio.h>

void func(int *x)
  {
   *x += 3;
   printf("%d\n",x);  //prints 13
  }

int main (void)
  {
   int x = 10;
   func(&x); 
   printf("%d\n",x);  //prints 13
   return 0;
  }

The reason this happens is that C does not know Pass By Reference. Everything is Pass By Value. When you call a C function you are passing in a copy of the variable, not the variable itself.  Even with pointers you are passing in a copy of the pointer... If you want to change the value of the pointer itself you need to pass in the address of the pointer; a pointer to a pointer... **x  ...

In C this is generally a good thing since it prevents same-name variables inside a function from accidentally affecting the overall program...

(Thanks aardvajk :D )

rkorko

yes - i understand now. thank you.
do you have a solution for the problem with calling a variable parameter function?