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.
#include <windows.h>
#include <iostream>
usingnamespace 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?
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
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).
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.
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.