How to Call a WINAPI Function

Please go easy on me, I have no idea how to code in WINAPI. I want to create a simple command line app that creates a shortcut to a file. I have to call the WINAPI function CreateHardLink() ( http://msdn.microsoft.com/en-us/library/Aa363860 ), but I don't know how to use it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <windows.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    void ShowUsage(char* executable);
    int MakeLink(char* src, char* dest);
    
    // Make sure user inputs 2 arguments
    if (argc != 3)
    {
        // We send the executable's name to ShowUsage in case it has been changed
        ShowUsage(argv[0]);
        return 1;
    }
    
    else
    {
        return MakeLink(argv[1], argv[2]);
    }
}

void ShowUsage(char* executable)
{
    cout << "\n\tUsage:  " << executable << " source destination\n" << endl;
}

int MakeLink(char* src, char* dest)
{
    // Attempt to create the shortcut and return error code to main program
    return CreateHardLink(src, dest); // I don't know how to call CreateHardLink()
}

int SendError(int code)
{
    cout << "Sending error " << code << endl;
    return code;
}


All of the WINAPI tutorials I have looked at show me how to build a window, but I just want to run this from the command line. Do I have to create a WinMain function?
Last edited on
Well, just read the page you linked. CreateHardLink takes three parameters (not two) and it says that the third must be NULL.
I still get the same problem adding NULL:

1
2
3
4
5
int MakeLink(char* src, char* dest)
{
    // Attempt to create the shortcut and return error code to main program
    return CreateHardLink(src, dest, NULL);
}


error: `CreateHardLink' was not declared in this scope
Last edited on
Ah, CreateHardLink is only available in Windows 2000 and later. If you want to use functions that didn't exist prior to Windows NT 4, you need to define the global macro _WIN32_WINNT as the minimum Windows version you want to target.
Windows 2000 is 0x500, XP is 0x501, Vista is 0x0600 and Windows 7 is 0x601.
As the MSDN page indicates, you just need Windows 2000 for CreateHardLink. So either add the line #define _WIN32_WINNT 0x500 before the first include of any Windows header or call the compiler with the parameter -D_WIN32_WINNT=0x500 - then this macro will be defined in all translation units.
If you use an IDE, you can use it to add global defines instead (in Code::Blocks: Project/Build options/#defines and add _WIN32_WINNT=0x500).
Last edited on
Thanks it compiles now. I am working on Windows XP, I guess my compiler (mingw32) just doesn't have that macro set by default. Or it could be that I'm not using the newest version of mingw32. Thanks again.
Last edited on
It is set by default, but to 0x400 (=Windows NT 4).
The development system generally isn't important and so the compiler doesn't care what OS you have. After all, the program is most likely supposed to run on other machines, which might have older versions of Windows.
I remember reading on msdn's website that the CreateHardLink function only works for NTFS filesystems, so that would explain why it requires the macro to be set.
Topic archived. No new replies allowed.