Re: small newbie question



Paw Suddergaard wrote:
I need to call a function in a dll... The prototype is:

INT MyFunction(PVOID buffer,PWORD BuffSize);

I've tried:

int bufSize = MyF_GetSize();
if(bufSize > 0){
char* t_Buffer = new char[bufSize];
if(MyFunction(Buffer ,(PWORD)bufSize)){
//success!
bool Ok = true;
}
}

It compiles, but doesn't seem to work.

Of course it compiles, because you used a big sledgehammer to force it
through the compiler. What you need to do is:

1. Remove all casts from your code, unless you really know what you are
doing those only serve to hide errors. The cast is what I meant with
sledgehammer above.

2. You will at first get errors (right so!) because you use the wrong types.
First, let's look at 'bufSize': in C++, generally, you should use a size_t
in order to store sizes, because that's what e.g. malloc(), sizeof,
strlen() etc work with. An 'int' is a) signed and b) too small on some
architectures.
Now, the function you have wants a 'PWORD'. This is the same as a 'WORD*',
i.e. a pointer to a WORD. So, simply use such a WORD (it is a signed,
integral 16 bit type) and pass the address (remember the '&' operator?) to
the function. Maybe you want to be sure and add some checking that your
size can be converted to the WORD without loss. Note that in this case you
are forced by the DLL's interface to write bad code, otherwise you would
use a size_t as described above.

Uli

.



Relevant Pages

  • Problem in compiling a C code with MSVC++6.00
    ... When I save the file as a cpp file, it compiles and runs ... int p1; ... void checkfile; ... printf("cannot allocate memory for elements!"); ...
    (comp.lang.c)
  • Re: Errors in compilation of simple programme
    ... using namespace std; ... cin>> i; ... it compiles without a problem on Visual C++ ... int main ...
    (microsoft.public.vc.language)
  • Re: Prime Numbers
    ... /*This code finally compiles as a 'c' code ... One more question....if i don't free up the memory allocated at the end ... it would appear that we are best served by an array ... prime numbers can be represented by an unsigned long int. ...
    (comp.lang.c)
  • Re: Errors in compilation of simple programme
    ... using namespace std; ... cin>> i; ... it compiles without a problem on Visual C++ ... int main ...
    (microsoft.public.vc.language)
  • Re: pthreads slows computer & kills program
    ... Expanded into something which actually compiles: ... int main ... the post but I thought I was dealing with experienced programmers. ... asked a recognizable question, just posted some code with prceious ...
    (comp.os.linux.development.apps)

Quantcast