Re: small newbie question
- From: Ulrich Eckhardt <eckhardt@xxxxxxxxxxxxxx>
- Date: Mon, 21 Aug 2006 16:18:56 +0200
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
.
- References:
- small newbie question
- From: Paw Suddergaard
- small newbie question
- Prev by Date: small newbie question
- Next by Date: Re: small newbie question
- Previous by thread: small newbie question
- Next by thread: Re: small newbie question
- Index(es):
Relevant Pages
|