Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 有没有办法在Windows7上以编程方式激活Windows_C_Windows_Winapi_Visual C++ - Fatal编程技术网

C 有没有办法在Windows7上以编程方式激活Windows

C 有没有办法在Windows7上以编程方式激活Windows,c,windows,winapi,visual-c++,C,Windows,Winapi,Visual C++,我正在进行一个项目,需要在Windows 7 PC上激活Windows。我使用Windows软件授权API编写了以下代码。MSDN表示下面使用的三个功能需要Windows 8或Windows Server 2012,这表明这些功能在Windows 7上不可用。但是,如果我在Windows 7计算机上的C:\Windows\System32中的depends.exe中打开slc.dll,我会执行SLOpen和SLClose函数。但不是SLActivateProduct 生成程序时,我收到链接器错误

我正在进行一个项目,需要在Windows 7 PC上激活Windows。我使用Windows软件授权API编写了以下代码。MSDN表示下面使用的三个功能需要Windows 8或Windows Server 2012,这表明这些功能在Windows 7上不可用。但是,如果我在Windows 7计算机上的C:\Windows\System32中的depends.exe中打开slc.dll,我会执行SLOpen和SLClose函数。但不是SLActivateProduct

生成程序时,我收到链接器错误:

1>  main.cpp
1>main.obj : error LNK2019: unresolved external symbol _SLActivateProduct@28 referenced in function _main
1>activate_windows.exe : fatal error LNK1120: 1 unresolved externals
这在链接时间是如何工作的?我猜想这意味着slc.lib(见下面的代码)没有SLActivateProduct函数

这个程序不需要用C语言编写,所以我可以从脚本中调用任何东西。有人知道在Windows7上是否有办法做到这一点吗?它必须是一种编程方式,可以通过运行脚本启动

代码如下:

#include <Windows.h>
#include <Slpublic.h>
#include <slerror.h>

#include <stdio.h>

// lib to use
#pragma comment(lib, "slc.lib")

int main() {

  // slmgr.vbs has:
  // private const WindowsAppId = "55c92734-d682-4d71-983e-d6ec3f16059f"
    // Windows AppId for SLID:  {55c92734-d682-4d71-983e-d6ec3f16059f} - this is a GUID
    const SLID SLID_WINDOWS = {0x55c92734, 0xd682, 0x4d71, 0x98, 0x3e, 0xd6, 0xec, 0x3f, 0x16, 0x05, 0x9f};

    HSLC hslc;
    HRESULT hr = SLOpen(&hslc);
    if (SUCCEEDED(hr))
    {
        // try to activate windows
        hr = SLActivateProduct(hslc, &SLID_WINDOWS, 0, NULL, NULL, NULL, 0);
        switch(hr) {
        case E_INVALIDARG:
            printf("SLActivateProduct - one or more arguments are not valid\n");
            break;
        case SL_E_PUBLISHING_LICENSE_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;
        case SL_E_PKEY_NOT_INSTALLED:
            printf("SLActivateProduct - The product key is not available\n");
            break;
        case SL_E_PRODUCT_SKU_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case S_OK:
            printf("SLActivateProduct - returned S_OK\n");
            break;
        default:
            printf("SLActivateProduct - unknown return code\n");
            break;
        }
        hr=SLClose(hslc);
    }
}
#包括
#包括
#包括
#包括
//要使用的库
#pragma注释(lib,“slc.lib”)
int main(){
//slmgr.vbs具有:
//private const WindowsAppId=“55c92734-d682-4d71-983e-d6ec3f16059f”
//Windows应用程序ID:{55c92734-d682-4d71-983e-d6ec3f16059f}-这是一个GUID
常量窗口={0x55c92734,0xd682,0x4d71,0x98,0x3e,0xd6,0xec,0x3f,0x16,0x05,0x9f};
HSLC-HSLC;
HRESULT hr=斜坡(&hslc);
如果(成功(hr))
{
//尝试激活windows
hr=SLActivateProduct(hslc和滑动窗口,0,NULL,NULL,NULL,0);
开关(hr){
案例E_INVALIDARG:
printf(“SLActivateProduct-一个或多个参数无效\n”);
打破
案例SL_E_发布许可证未安装:
printf(“SLActivateProduct-未安装许可证\n”);
打破
案例SL_E_PKEY_未安装:
printf(“SLActivateProduct-产品密钥不可用\n”);
打破
未安装SLU E_产品SKU_的情况:
printf(“SLActivateProduct-未安装许可证\n”);
打破
案例S_OK:
printf(“SLActivateProduct-返回的S_OK\n”);
打破
违约:
printf(“SLActivateProduct-未知返回码\n”);
打破
}
hr=SLClose(hslc);
}
}
slactivatedproduct()
在Windows 7上根本不可用,即使您可以在编译时链接到它(显然您不能,因为您的
slc.lib
版本缺少该引用)

但是,您可以使用
GetProcAddress()
在运行时动态加载
SLActivateProduct()
。这将使您克服链接器错误(直到您更新开发环境),并至少允许您的代码在Windows 8及更高版本上工作

仅供参考,
slidefs.h
定义了一个
WINDOWS\u slided
常量,因此您不需要在代码中手动定义它

尝试类似以下内容:

#include <Windows.h>
#include <Slpublic.h>
#include <slerror.h>
#include <sliddefs.h>

#include <stdio.h>

// lib to use
#pragma comment(lib, "slc.lib")

// if your version of Slpublic.h does not define these then uncomment this...
/*
typedef enum _tagSL_ACTIVATION_TYPE { 
  SL_ACTIVATION_TYPE_DEFAULT           = 0,
  SL_ACTIVATION_TYPE_ACTIVE_DIRECTORY  = 1
} SL_ACTIVATION_TYPE;

typedef struct _tagSL_ACTIVATION_INFO_HEADER {
  DWORD              cbSize;
  SL_ACTIVATION_TYPE type;
} SL_ACTIVATION_INFO_HEADER;
*/

typedef HRESULT WINAPI (*LPFN_SLActivateProduct)(HSLC, const SLID*, UINT, const PVOID, const SL_ACTIVATION_INFO_HEADER*, PCWSTR, WORD);

int main()
{
    LPFN_SLActivateProduct lpSLActivateProduct = (LPFN_SLActivateProduct) GetProcAddress(GetModuleHandle(TEXT("slc.dll")), "SLActivateProduct");
    if (!lpSLActivateProduct)
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }

    HSLC hslc;

    HRESULT hr = SLOpen(&hslc);
    if (FAILED(hr))
    {
        printf("SLOpen - unknown return code 0x%08X\n", hr);
        return 0;
    }

    // try to activate windows
    hr = lpSLActivateProduct(hslc, &WINDOWS_SLID, 0, NULL, NULL, NULL, 0);
    switch(hr)
    {
        case E_INVALIDARG:
            printf("SLActivateProduct - one or more arguments are not valid\n");
            break;

        case SL_E_PUBLISHING_LICENSE_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case SL_E_PKEY_NOT_INSTALLED:
            printf("SLActivateProduct - The product key is not available\n");
            break;

        case SL_E_PRODUCT_SKU_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case S_OK:
            printf("SLActivateProduct - returned S_OK\n");
            break;

        default:
            printf("SLActivateProduct - unknown return code 0x%08X\n", hr);
            break;
    }

    hr = SLClose(hslc);

    return 0;
}

更新:如果以后您更新到一个开发环境,该环境的
slc.lib
不再缺少对
slactivatedproduct()
的引用,您可以删除对
GetProcAddress()
的显式调用,而使用链接器的。这使您可以像编写静态链接一样编写代码,但是链接器会插入特殊代码,以便在运行时第一次调用DLL函数时为您调用
GetProcAddress()
。这将允许您在“静态”调用函数之前检查操作系统版本,允许您的应用程序仍在旧版本的Windows上运行,但仅在Windows 7+上执行激活

#include <Windows.h>
#include <Slpublic.h>
#include <slerror.h>
#include <sliddefs.h>

#include <stdio.h>

// lib to use
#pragma comment(lib, "slc.lib")

// if your version of Slpublic.h does not define these then uncomment this...
/*
typedef enum _tagSL_ACTIVATION_TYPE { 
  SL_ACTIVATION_TYPE_DEFAULT           = 0,
  SL_ACTIVATION_TYPE_ACTIVE_DIRECTORY  = 1
} SL_ACTIVATION_TYPE;

typedef struct _tagSL_ACTIVATION_INFO_HEADER {
  DWORD              cbSize;
  SL_ACTIVATION_TYPE type;
} SL_ACTIVATION_INFO_HEADER;
*/

int main()
{
    OSVERSIONINFO osi = {0};
    osi.dwOSVersionInfoSize = sizeof(osi).
    GetVersionEx(&osi);

    if ((osi.dwMajorVersion < 6) || ((osi.dwMajorVersion == 6) && (dwMinorVersion < 1)))
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }

    // alternatively:
    /*
    #include <VersionHelpers.h>

    if (!IsWindows7OrGreater())
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }
    */

    HSLC hslc;

    HRESULT hr = SLOpen(&hslc);
    if (FAILED(hr))
    {
        printf("SLOpen - unknown return code 0x%08X\n", hr);
        return 0;
    }

    // try to activate windows
    hr = SLActivateProduct(hslc, &WINDOWS_SLID, 0, NULL, NULL, NULL, 0);
    switch(hr)
    {
        case E_INVALIDARG:
            printf("SLActivateProduct - one or more arguments are not valid\n");
            break;

        case SL_E_PUBLISHING_LICENSE_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case SL_E_PKEY_NOT_INSTALLED:
            printf("SLActivateProduct - The product key is not available\n");
            break;

        case SL_E_PRODUCT_SKU_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case S_OK:
            printf("SLActivateProduct - returned S_OK\n");
            break;

        default:
            printf("SLActivateProduct - unknown return code 0x%08X\n", hr);
            break;
    }

    hr = SLClose(hslc);

    return 0;
}
slactivatedproduct()
在Windows 7上根本不可用,即使您可以在编译时链接到它(显然您不能,因为您的
slc.lib
版本缺少该引用)

但是,您可以使用
GetProcAddress()
在运行时动态加载
SLActivateProduct()
。这将使您克服链接器错误(直到您更新开发环境),并至少允许您的代码在Windows 8及更高版本上工作

仅供参考,
slidefs.h
定义了一个
WINDOWS\u slided
常量,因此您不需要在代码中手动定义它

尝试类似以下内容:

#include <Windows.h>
#include <Slpublic.h>
#include <slerror.h>
#include <sliddefs.h>

#include <stdio.h>

// lib to use
#pragma comment(lib, "slc.lib")

// if your version of Slpublic.h does not define these then uncomment this...
/*
typedef enum _tagSL_ACTIVATION_TYPE { 
  SL_ACTIVATION_TYPE_DEFAULT           = 0,
  SL_ACTIVATION_TYPE_ACTIVE_DIRECTORY  = 1
} SL_ACTIVATION_TYPE;

typedef struct _tagSL_ACTIVATION_INFO_HEADER {
  DWORD              cbSize;
  SL_ACTIVATION_TYPE type;
} SL_ACTIVATION_INFO_HEADER;
*/

typedef HRESULT WINAPI (*LPFN_SLActivateProduct)(HSLC, const SLID*, UINT, const PVOID, const SL_ACTIVATION_INFO_HEADER*, PCWSTR, WORD);

int main()
{
    LPFN_SLActivateProduct lpSLActivateProduct = (LPFN_SLActivateProduct) GetProcAddress(GetModuleHandle(TEXT("slc.dll")), "SLActivateProduct");
    if (!lpSLActivateProduct)
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }

    HSLC hslc;

    HRESULT hr = SLOpen(&hslc);
    if (FAILED(hr))
    {
        printf("SLOpen - unknown return code 0x%08X\n", hr);
        return 0;
    }

    // try to activate windows
    hr = lpSLActivateProduct(hslc, &WINDOWS_SLID, 0, NULL, NULL, NULL, 0);
    switch(hr)
    {
        case E_INVALIDARG:
            printf("SLActivateProduct - one or more arguments are not valid\n");
            break;

        case SL_E_PUBLISHING_LICENSE_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case SL_E_PKEY_NOT_INSTALLED:
            printf("SLActivateProduct - The product key is not available\n");
            break;

        case SL_E_PRODUCT_SKU_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case S_OK:
            printf("SLActivateProduct - returned S_OK\n");
            break;

        default:
            printf("SLActivateProduct - unknown return code 0x%08X\n", hr);
            break;
    }

    hr = SLClose(hslc);

    return 0;
}

更新:如果以后您更新到一个开发环境,该环境的
slc.lib
不再缺少对
slactivatedproduct()
的引用,您可以删除对
GetProcAddress()
的显式调用,而使用链接器的。这使您可以像编写静态链接一样编写代码,但是链接器会插入特殊代码,以便在运行时第一次调用DLL函数时为您调用
GetProcAddress()
。这将允许您在“静态”调用函数之前检查操作系统版本,允许您的应用程序仍在旧版本的Windows上运行,但仅在Windows 7+上执行激活

#include <Windows.h>
#include <Slpublic.h>
#include <slerror.h>
#include <sliddefs.h>

#include <stdio.h>

// lib to use
#pragma comment(lib, "slc.lib")

// if your version of Slpublic.h does not define these then uncomment this...
/*
typedef enum _tagSL_ACTIVATION_TYPE { 
  SL_ACTIVATION_TYPE_DEFAULT           = 0,
  SL_ACTIVATION_TYPE_ACTIVE_DIRECTORY  = 1
} SL_ACTIVATION_TYPE;

typedef struct _tagSL_ACTIVATION_INFO_HEADER {
  DWORD              cbSize;
  SL_ACTIVATION_TYPE type;
} SL_ACTIVATION_INFO_HEADER;
*/

int main()
{
    OSVERSIONINFO osi = {0};
    osi.dwOSVersionInfoSize = sizeof(osi).
    GetVersionEx(&osi);

    if ((osi.dwMajorVersion < 6) || ((osi.dwMajorVersion == 6) && (dwMinorVersion < 1)))
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }

    // alternatively:
    /*
    #include <VersionHelpers.h>

    if (!IsWindows7OrGreater())
    {
        printf("SLActivateProduct - The product cannot be activated on this system\n");
        return 0;
    }
    */

    HSLC hslc;

    HRESULT hr = SLOpen(&hslc);
    if (FAILED(hr))
    {
        printf("SLOpen - unknown return code 0x%08X\n", hr);
        return 0;
    }

    // try to activate windows
    hr = SLActivateProduct(hslc, &WINDOWS_SLID, 0, NULL, NULL, NULL, 0);
    switch(hr)
    {
        case E_INVALIDARG:
            printf("SLActivateProduct - one or more arguments are not valid\n");
            break;

        case SL_E_PUBLISHING_LICENSE_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case SL_E_PKEY_NOT_INSTALLED:
            printf("SLActivateProduct - The product key is not available\n");
            break;

        case SL_E_PRODUCT_SKU_NOT_INSTALLED:
            printf("SLActivateProduct - The license is not installed\n");
            break;

        case S_OK:
            printf("SLActivateProduct - returned S_OK\n");
            break;

        default:
            printf("SLActivateProduct - unknown return code 0x%08X\n", hr);
            break;
    }

    hr = SLClose(hslc);

    return 0;
}

文档中说,您尝试使用的是Windows 8及更高版本。根据你的作品,你知道情况就是这样。你仍然坚持在Windows7中使用这些功能。您似乎对代码未能链接感到惊讶(显然b/c函数在库中找不到)。既然你已经知道你想用的东西在Windows 7中是不可用的,为什么还要坚持甚至发帖呢?@ray正确,但我想知道有什么办法可以做到。使用一个已经准备好分发的Windows映像,同时使用一个已经激活的Windows?@RedX,这会有什么不同吗?系统准备过程被认为是对已部署映像的概括。@ray:MSDN文档在API支持的最低操作系统版本方面经常存在误导。例如,MSDN说
SLOpen()
SLClose()
仅在W上可用