Win32

 view release on metacpan or  search on metacpan

Win32.xs  view on Meta::CPAN

            }
        } while (!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

        if (!ret) {
            SvREFCNT_dec((SV*)priv_hv);
            Safefree(privs);
            Safefree(priv_name);
            XSRETURN_NO;
        }

        hv_store(priv_hv, priv_name, ret_len, newSViv(is_enabled), 0);
    }

    Safefree(privs);
    Safefree(priv_name);

    ST(0) = sv_2mortal(newRV_noinc((SV*)priv_hv));
    XSRETURN(1);
}

XS(w32_IsDeveloperModeEnabled)
{
    dXSARGS;
    LONG status;
    DWORD val, val_size = sizeof(val);
    PFNRegGetValueA pfnRegGetValueA;
    HMODULE module;

    if (items)
        Perl_croak(aTHX_ "usage: Win32::IsDeveloperModeEnabled()");

    EXTEND(SP, 1);

    /* developer mode was introduced in Windows 10 */
    if (g_osver.dwMajorVersion < 10)
        XSRETURN_NO;

    module = GetModuleHandleA("advapi32.dll");
    GETPROC(RegGetValueA);
    if (!pfnRegGetValueA)
        XSRETURN_NO;

    status = pfnRegGetValueA(
        HKEY_LOCAL_MACHINE,
        "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock",
        "AllowDevelopmentWithoutDevLicense",
        RRF_RT_REG_DWORD | RRF_SUBKEY_WOW6464KEY,
        NULL,
        &val,
        &val_size
    );

    if (status == ERROR_SUCCESS && val == 1)
        XSRETURN_YES;

    XSRETURN_NO;
}

#ifdef WINHTTPAPI

XS(w32_HttpGetFile)
{
    dXSARGS;
    WCHAR *url = NULL, *file = NULL, *hostName = NULL, *urlPath = NULL;
    bool bIgnoreCertErrors = FALSE;
    WCHAR msgbuf[ONE_K_BUFSIZE];
    BOOL  bResults = FALSE;
    HINTERNET  hSession = NULL,
               hConnect = NULL,
               hRequest = NULL;
    HANDLE hOut = INVALID_HANDLE_VALUE;
    BOOL   bParsed = FALSE,
           bAborted = FALSE,
           bFileError = FALSE,
           bHttpError = FALSE;
    DWORD error = 0;
    URL_COMPONENTS urlComp;
    LPCWSTR acceptTypes[] = { L"*/*", NULL };
    DWORD dwHttpStatusCode = 0, dwQuerySize = 0;

    if (items < 2 || items > 3)
        croak("usage: Win32::HttpGetFile($url, $file[, $ignore_cert_errors])");

    url = sv_to_wstr(aTHX_ ST(0));
    file = sv_to_wstr(aTHX_ ST(1));

    if (items == 3)
        bIgnoreCertErrors = (BOOL)SvIV(ST(2));

    /* Initialize the URL_COMPONENTS structure, setting the required
     * component lengths to non-zero so that they get populated.
     */
    ZeroMemory(&urlComp, sizeof(urlComp));
    urlComp.dwStructSize = sizeof(urlComp);
    urlComp.dwSchemeLength    = (DWORD)-1;
    urlComp.dwHostNameLength  = (DWORD)-1;
    urlComp.dwUrlPathLength   = (DWORD)-1;
    urlComp.dwExtraInfoLength = (DWORD)-1;

    /* Parse the URL. */
    bParsed = WinHttpCrackUrl(url, (DWORD)wcslen(url), 0, &urlComp);

    /* Only support http and htts, not ftp, gopher, etc. */
    if (bParsed
        && !(urlComp.nScheme == INTERNET_SCHEME_HTTPS
             || urlComp.nScheme == INTERNET_SCHEME_HTTP)) {
        SetLastError(12006); /* not a recognized protocol */
        bParsed = FALSE;
    }

    if (bParsed) {
        New(0, hostName,  urlComp.dwHostNameLength + 1, WCHAR);
        wcsncpy(hostName, urlComp.lpszHostName, urlComp.dwHostNameLength);
        hostName[urlComp.dwHostNameLength] = 0;

        New(0, urlPath,  urlComp.dwUrlPathLength + urlComp.dwExtraInfoLength + 1, WCHAR);
        wcsncpy(urlPath, urlComp.lpszUrlPath, urlComp.dwUrlPathLength + urlComp.dwExtraInfoLength);
        urlPath[urlComp.dwUrlPathLength + urlComp.dwExtraInfoLength] = 0;

        /* Use WinHttpOpen to obtain a session handle. */
        hSession = WinHttpOpen(L"Perl",
                               WINHTTP_ACCESS_TYPE_NO_PROXY,
                               WINHTTP_NO_PROXY_NAME,
                               WINHTTP_NO_PROXY_BYPASS,
                               0);
    }

    /* Specify an HTTP server. */
    if (hSession)
        hConnect = WinHttpConnect(hSession,
                                  hostName,
                                  urlComp.nPort,
                                  0);

    /* Create an HTTP request handle. */
    if (hConnect)
        hRequest = WinHttpOpenRequest(hConnect,
                                      L"GET",
                                      urlPath,
                                      NULL,
                                      WINHTTP_NO_REFERER,
                                      acceptTypes,
                                      urlComp.nScheme == INTERNET_SCHEME_HTTPS
                                                      ? WINHTTP_FLAG_SECURE
                                                      : 0);

    /* If specified, disable certificate-related errors for https connections. */
    if (hRequest
        && bIgnoreCertErrors
        && urlComp.nScheme == INTERNET_SCHEME_HTTPS) {
        DWORD secFlags = SECURITY_FLAG_IGNORE_CERT_CN_INVALID
                         | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
                         | SECURITY_FLAG_IGNORE_UNKNOWN_CA
                         | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
        if(!WinHttpSetOption(hRequest,
                             WINHTTP_OPTION_SECURITY_FLAGS,
                             &secFlags,
                             sizeof(secFlags))) {
            bAborted = TRUE;
        }
    }

    /* Call WinHttpGetProxyForUrl with our target URL. If auto-proxy succeeds,
     * then set the proxy info on the request handle. If auto-proxy fails,
     * ignore the error and attempt to send the HTTP request directly to the
     * target server (using the default WINHTTP_ACCESS_TYPE_NO_PROXY
     * configuration, which the request handle will inherit from the session).
     */
    if (hRequest && !bAborted) {
        WINHTTP_AUTOPROXY_OPTIONS  AutoProxyOptions;
        WINHTTP_PROXY_INFO         ProxyInfo;
        DWORD                      cbProxyInfoSize = sizeof(ProxyInfo);

        ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
        ZeroMemory(&ProxyInfo, sizeof(ProxyInfo));
        AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
        AutoProxyOptions.dwAutoDetectFlags =
                                    WINHTTP_AUTO_DETECT_TYPE_DHCP |
                                    WINHTTP_AUTO_DETECT_TYPE_DNS_A;
        AutoProxyOptions.fAutoLogonIfChallenged = TRUE;

        if(WinHttpGetProxyForUrl(hSession,
                                url,
                                &AutoProxyOptions,
                                &ProxyInfo)) {
            if(!WinHttpSetOption(hRequest,
                                WINHTTP_OPTION_PROXY,
                                &ProxyInfo,
                                cbProxyInfoSize)) {
                bAborted = TRUE;
                Perl_warn(aTHX_ "Win32::HttpGetFile: setting proxy options failed");
            }
            /* WinHttpGetProxyForUrl allocates these with GlobalAlloc. */
            if (ProxyInfo.lpszProxy)       GlobalFree(ProxyInfo.lpszProxy);
            if (ProxyInfo.lpszProxyBypass) GlobalFree(ProxyInfo.lpszProxyBypass);
        }
    }

    /* Send a request. */
    if (hRequest && !bAborted)
        bResults = WinHttpSendRequest(hRequest,
                                      WINHTTP_NO_ADDITIONAL_HEADERS,
                                      0,
                                      WINHTTP_NO_REQUEST_DATA,
                                      0,
                                      0,
                                      0);

    /* End the request. */
    if (bResults)
        bResults = WinHttpReceiveResponse(hRequest, NULL);

    /* Retrieve HTTP status code. */
    if (bResults) {
        dwQuerySize = sizeof(dwHttpStatusCode);
        bResults = WinHttpQueryHeaders(hRequest,
                                       WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER,
                                       WINHTTP_HEADER_NAME_BY_INDEX,
                                       &dwHttpStatusCode,
                                       &dwQuerySize,
                                       WINHTTP_NO_HEADER_INDEX);
    }

    /* Retrieve HTTP status text. Note this may be a success message. */
    if (bResults) {
        dwQuerySize = ONE_K_BUFSIZE * 2 - 2;
        ZeroMemory(&msgbuf, ONE_K_BUFSIZE * 2);
        bResults = WinHttpQueryHeaders(hRequest,
                                       WINHTTP_QUERY_STATUS_TEXT,
                                       WINHTTP_HEADER_NAME_BY_INDEX,
                                       msgbuf,
                                       &dwQuerySize,
                                       WINHTTP_NO_HEADER_INDEX);
    }

    /* There is no point in successfully downloading an error page from
     * the server, so consider HTTP errors to be failures.
     */
    if (bResults) {
        if (dwHttpStatusCode < 200 || dwHttpStatusCode > 299) {
            bResults = FALSE;
            bHttpError = TRUE;
        }
    }

    /* Create output file for download. */
    if (bResults) {
        hOut = CreateFileW(file,
                           GENERIC_WRITE,
                           FILE_SHARE_READ | FILE_SHARE_WRITE,
                           NULL,

Win32.xs  view on Meta::CPAN


    newXS("Win32::LookupAccountName", w32_LookupAccountName, file);
    newXS("Win32::LookupAccountSID", w32_LookupAccountSID, file);
    newXS("Win32::InitiateSystemShutdown", w32_InitiateSystemShutdown, file);
    newXS("Win32::AbortSystemShutdown", w32_AbortSystemShutdown, file);
    newXS("Win32::ExpandEnvironmentStrings", w32_ExpandEnvironmentStrings, file);
    newXS("Win32::MsgBox", w32_MsgBox, file);
    newXS("Win32::LoadLibrary", w32_LoadLibrary, file);
    newXS("Win32::FreeLibrary", w32_FreeLibrary, file);
    newXS("Win32::GetProcAddress", w32_GetProcAddress, file);
    newXS("Win32::RegisterServer", w32_RegisterServer, file);
    newXS("Win32::UnregisterServer", w32_UnregisterServer, file);
    newXS("Win32::GetArchName", w32_GetArchName, file);
    newXS("Win32::GetChipArch", w32_GetChipArch, file);
    newXS("Win32::GetChipName", w32_GetChipName, file);
    newXS("Win32::GuidGen", w32_GuidGen, file);
    newXS("Win32::GetFolderPath", w32_GetFolderPath, file);
    newXS("Win32::IsAdminUser", w32_IsAdminUser, file);
    newXS("Win32::GetFileVersion", w32_GetFileVersion, file);

    newXS("Win32::GetCwd", w32_GetCwd, file);
    newXS("Win32::SetCwd", w32_SetCwd, file);
    newXS("Win32::GetNextAvailDrive", w32_GetNextAvailDrive, file);
    newXS("Win32::GetLastError", w32_GetLastError, file);
    newXS("Win32::SetLastError", w32_SetLastError, file);
    newXS("Win32::LoginName", w32_LoginName, file);
    newXS("Win32::NodeName", w32_NodeName, file);
    newXS("Win32::DomainName", w32_DomainName, file);
    newXS("Win32::FsType", w32_FsType, file);
    newXS("Win32::GetOSVersion", w32_GetOSVersion, file);
    newXS("Win32::IsWinNT", w32_IsWinNT, file);
    newXS("Win32::IsWin95", w32_IsWin95, file);
    newXS("Win32::FormatMessage", w32_FormatMessage, file);
    newXS("Win32::Spawn", w32_Spawn, file);
    newXS("Win32::GetTickCount", w32_GetTickCount, file);
    newXS("Win32::GetShortPathName", w32_GetShortPathName, file);
    newXS("Win32::GetFullPathName", w32_GetFullPathName, file);
    newXS("Win32::GetLongPathName", w32_GetLongPathName, file);
    newXS("Win32::GetANSIPathName", w32_GetANSIPathName, file);
    newXS("Win32::CopyFile", w32_CopyFile, file);
    newXS("Win32::Sleep", w32_Sleep, file);
    newXS("Win32::OutputDebugString", w32_OutputDebugString, file);
    newXS("Win32::GetCurrentProcessId", w32_GetCurrentProcessId, file);
    newXS("Win32::GetCurrentThreadId", w32_GetCurrentThreadId, file);
    newXS("Win32::CreateDirectory", w32_CreateDirectory, file);
    newXS("Win32::CreateFile", w32_CreateFile, file);
    newXS("Win32::GetSystemMetrics", w32_GetSystemMetrics, file);
    newXS("Win32::GetProductInfo", w32_GetProductInfo, file);
    newXS("Win32::GetACP", w32_GetACP, file);
    newXS("Win32::GetConsoleCP", w32_GetConsoleCP, file);
    newXS("Win32::GetConsoleOutputCP", w32_GetConsoleOutputCP, file);
    newXS("Win32::GetOEMCP", w32_GetOEMCP, file);
    newXS("Win32::SetConsoleCP", w32_SetConsoleCP, file);
    newXS("Win32::SetConsoleOutputCP", w32_SetConsoleOutputCP, file);
    newXS("Win32::GetProcessPrivileges", w32_GetProcessPrivileges, file);
    newXS("Win32::IsDeveloperModeEnabled", w32_IsDeveloperModeEnabled, file);
#ifdef __CYGWIN__
    newXS("Win32::SetChildShowWindow", w32_SetChildShowWindow, file);
#endif
#ifdef WINHTTPAPI
    newXS("Win32::HttpGetFile", w32_HttpGetFile, file);
#endif
    XSRETURN_YES;
}



( run in 0.490 second using v1.01-cache-2.11-cpan-2398b32b56e )