Using external dll

Hi.

I want to use an external dll function which searches the screen for an image.
It isnt a dll found native in a windows system.
I have succesfully done this before, but it came with all nececssary files I needed, such as headers and a lib file, along with some pretty detailed instructions on how to use it.

This dll however does not come with anything (well apart from the compiled dll and the source) so Im basically stumped on how to proceed.

Do all dll files need a lib to be linked?

The dll is ImageSearchDll.dll and has just one function, and I believe it also uses gdi+ dll.

I would welcome any examples or advice, however small or detailed.

Thanks for reading.
Last edited on
If it comes with the source, you have the header file and probably everything else. But if only has one function, it makes the task it easier that one bit easier.

The first path is to build the DLL from source. That should generate a .lib file to link to, and you'll already have a header file with the function prototype. So that's that.

The second path is to just use the DLL directly. You'll have to make up a prototype. Consult the source to see what the actual function prototype is.

Let's say it's BOOL ImageSearch(LPCTSTR, LPSTR, DWORD);. You can dynamically load it as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
typedef BOOL ImageSearchFunc(LPCTSTR, LPSTR, DWORD);
ImageSearchFunc* ImageSearch = 0;
HINSTANCE hImageSearchDLL = 0;

BOOL Load()
{
    hImangeSearchDLL = LoadLibrary("ImageSearchDll.dll");
    if (hImangeSearchDLL)
    {
        ImageSearch =
            (ImageSearchFunc*)GetProcAddress(hImangeSearchDLL, "ImageSearch");
    }

    return hImangeSearchDLL != 0  &&  ImageSearchFunc != 0;
}

void Unload()
{
    if (hImageSearchDLL)
        FreeLibrary(hImageSearchDLL);

    hImageSearchDLL = 0;
    ImageSearch = 0;
}


and use it:
1
2
    if (ImageSearch)
        ImageSearch(<<your parameters>>);


This hasn't been compiled so it'll have errors.
HI kbw.

Just to aknowledge your post.

Thanks, I will try this asap.

Suze.
Hello again, I have been looking closer at the example give, and i'm grateful for it.
Im struggling with a problem though, The dll returns a string and I'm unsure how to declare a function when its return value is a string, as far as I know, C++ uses an array of chars for a string.

char string[128] // holds 127 charactors

Is this valid for use with a function?

edit-

I see a function cannot return an array :(
Last edited on
To the best of my understanding, functions can't return strings. However, they can return a pointer to the beginning of a string.

You can read Chapter 4 of the book at
http://msdn.microsoft.com/en-us/beginner/cc305129.aspx
for details.

Suzie-
Although functions can't return arrays, they can return a pointer to the beginning of an array. That's almost the same thing in practice. If you read over that Chapter 4, it should explain a good bit.
Last edited on
lol

I've been back on that chapter, re-reading it, I read them all then again, and am trying to refer to them as I go along.

However I'm still unsure then, as to how to retrieve the returnd string from the dll function, Im assuming I need that in order to get a pointer to it.

Then return the pointer?

Or am I wide of the mark?
I've never really used C++ before. Just read over that book and some other materials, so I'm basically going by what I think I remember. That said, this is what I think is right.

If you want to get the return value, you can declare a char pointer, such as:
char *returnValue;

Then you can access the returned string as:
1
2
theFirstLetterInYourString = returnValue[0]
theSecondLetterInYourString = returnValue[1]


So you'd have:
1
2
3
4
5
6
7
8
char *myString;

myString = MyFunctionFromTheDLL(args);

theFirstCharacterInMyString  = myString[0];
theSecondCharacterInMyString = myString[1];
theThirdCharacterInMyString  = myString[2];
//etc. 
Last edited on
If you're trying to understand the example, don't get bogged down on strings. I was trying to demonstrate how to dynamically load a function from a DLL. If you don't understand C strings, you're going to struggle, so take it one step at a time.

Strings
The C Programming Language is a systems programming language and has data types that matched the hardware at the time of invention. So it has char for a machine byte, int for a machine word and float/double for different precision floating point numbers. But a machine has no string type and neither does C.

The decision was made to encode strings as a null terminated block of char. And a set of functions was provided to manipulate these strings, strcpy, strcat, strlen and so on. So C strings are implemented in the C runtime library, but the implementation is exposed.

When writing a function to receive a C string, it should pass the buffer and its size. Failure to consider the size exposes you to the possibility of buffer overrun.

The C++ string is a class and its implementation is hidden. But it has constructors and methods that ease interoperability with C strings.
Last edited on
Thanks again.

I'l get my head back into the tutorials and try to understand it more clearly.
Topic archived. No new replies allowed.