Created
September 19, 2020 09:45
-
-
Save nta/0e9fd562960df992a3fc1daef68e1218 to your computer and use it in GitHub Desktop.
DNS-SD service registration for Windows 10 to show Apple device icons on macOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <windows.h> | |
#include <windns.h> | |
#pragma comment(lib, "dnsapi.lib") | |
#pragma comment(lib, "ws2_32.lib") | |
static void Done(DWORD, PVOID, PDNS_SERVICE_INSTANCE) | |
{ | |
} | |
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) | |
{ | |
int argc = 0; | |
wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc); | |
const wchar_t* model = L"MacPro7,1"; | |
const wchar_t* color = L""; | |
if (argc >= 2) | |
{ | |
model = argv[1]; | |
} | |
if (argc >= 3) | |
{ | |
color = argv[2]; | |
} | |
int keyCount = (color && color[0]) ? 2 : 1; | |
const wchar_t* keys[] = { L"model", L"ecolor" }; | |
const wchar_t* values[] = { model, color }; | |
wchar_t computername[512]; | |
DWORD nameLen = _countof(computername); | |
GetComputerNameW(computername, &nameLen); | |
auto reg = [&](const wchar_t* host, int port, bool mac) | |
{ | |
wchar_t hostBuf[256]; | |
swprintf_s(hostBuf, L"%s%s", computername, host); | |
auto instance = DnsServiceConstructInstance( | |
hostBuf, | |
computername, | |
NULL, | |
NULL, | |
port, | |
0, | |
0, | |
mac ? keyCount : 0, | |
mac ? keys : nullptr, | |
mac ? values : nullptr); | |
DNS_SERVICE_REGISTER_REQUEST regReq = { 0 }; | |
regReq.Version = DNS_QUERY_REQUEST_VERSION1; | |
regReq.InterfaceIndex = 0; | |
regReq.pServiceInstance = instance; | |
regReq.unicastEnabled = FALSE; | |
regReq.pRegisterCompletionCallback = Done; | |
DnsServiceRegister(®Req, NULL); | |
}; | |
reg(L"._device-info._tcp.local", 696, true); | |
reg(L"._smb._tcp.local", 445, false); | |
WaitForSingleObject(GetCurrentProcess(), INFINITE); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment