如何从C ++ Console应用程序中创建dll,可以在C#aplication中使用? LNK2019 Win32 DLL

时间:2017-05-18 12:34:08

标签: c++ dll wsdl ws-discovery

我想用WS-Discovery功能制作dll。 我的模板是这个Microsoft示例项目。 https://code.msdn.microsoft.com/windowsapps/WS-Discovery-SDK-Sample-8138b0d1/view/SourceCode 内部解决方案有3个项目(客户端,通用和服务) 我可以编译并运行所有这些并且它们工作正常。

我使用这些参数运行服务器(我在http& https之后添加了空格): WSDiscoveryService.exe / a http://192.168.2.53/onvif/device_service / s https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl https://www.onvif.org/ver20/ptz /wsdl/ptz.wsdl https://www.onvif.org/ver10/media/wsdl/media.wsdl https://www.onvif.org/ver10/events/wsdl/event.wsdl

一切都很好,直到现在。

现在我已将新项目添加到解决方案ServiceDll(Win32项目,应用程序类型DLL) 我在Common项目中添加了Common.h和Common.cpp文件,在Service项目中添加了TargetService.h和TargetService.cpp。 然后我在ServiceDll.cpp

中添加此代码
// ServiceDll.cpp : Defines the exported functions for the DLL application.

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <strsafe.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <wsdapi.h>
#include "Common.h"
#include "TargetService.h"

#if !defined(WS_DISCOVERY)

CTargetService *service = NULL;

// Exspose 5 methods to outside. (prototypes)

// create service and start lissen for probe messages.
// if return value is 1 service is created and active.
// if return value is 0 something is wrong.
extern "C" __declspec(dllexport) int __cdecl WSDiscoveryCreate(LPWSTR endpointReference,WSD_URI_LIST *scopes);

// get Endpoint reference of service
// if return string is 0 then service is not running.
extern "C" __declspec(dllexport) LPCWSTR __cdecl WSDiscoveryGetEndpointReference();

// send hello message
// if return value is 1 helo message was sended.
// if return value is 0 something is wrong.
extern "C" __declspec(dllexport) int __cdecl WSDiscoveryHello();

// send bye message, this don't stop the service.
// if return value is 1 bye message was sended.
// if return value is 0 something is wrong.
extern "C" __declspec(dllexport) int __cdecl WSDiscoveryBye();

// stop service and dispose used objects
// if return value is 1 service was disposed or service was not created in first place.
// if return value is 0 something is wrong.
extern "C" __declspec(dllexport) int __cdecl WSDiscoveryDispose();


// (inplementation)
extern "C" __declspec(dllexport) int __cdecl WSDiscoveryCreate(LPWSTR endpointReference,WSD_URI_LIST *scopes)
{
 HRESULT hr = S_OK;
 hr = CreateTargetService(endpointReference, scopes, &service);
 if (hr == S_OK)
 {
    return 1;
 }
 else
 {
    return 0;
 }
}

extern "C" __declspec(dllexport) LPCWSTR __cdecl WSDiscoveryGetEndpointReference()
{
 LPCWSTR temp = L"0";
 HRESULT hr = S_FALSE;
 if (NULL != service)
 {
    hr = service->GetEndpointReference(&temp);
    if (hr == S_OK)
    {
        return temp;
    }
 }
 return temp;
}

extern "C" __declspec(dllexport) int __cdecl WSDiscoveryHello()
{
 if (NULL != service)
 {
    HRESULT hr = service->SendHelloMessage();
    if (hr == S_OK)
    {
        return 1;
    }
 }
 return 0;
}

extern "C" __declspec(dllexport) int __cdecl WSDiscoveryBye()
{
 if (NULL != service)
 {
    HRESULT hr = service->SendByeMessage();
    if (hr == S_OK)
    {
        return 1;
    }
 }
 return 0;
}

extern "C" __declspec(dllexport) int __cdecl WSDiscoveryDispose()
{
 if (NULL != service)
 {
    HRESULT hr = service->Terminate();
    if (hr == S_OK)
    {
        if (NULL != service)
        {
            service->Release();
            service = NULL;
        }
        return 1;
    }
    else
    {
        return 0;
    }
 }
 else
 {
    // service was not created.
    return 1;
 } 
}

#endif /* !defined(WS_DISCOVERY) */

现在我尝试构建此DLL,我得到11个错误: 8x LNK2019 2x LNK2001 1x LNK1120

严重级代码描述项目文件行抑制状态 错误LNK2019未解析的外部符号__imp__gethostname @ 8在函数&#34中引用; long __cdecl GetWideStringHostName(wchar_t * *)&#34; (?GetWideStringHostName @@ YAJPAPA_W @ Z)ServiceDll C:\ test \ C ++ \ ServiceDll \ Common.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2001未解析的外部符号_WSDAllocateLinkedMemory @ 8 ServiceDll C:\ test \ C ++ \ ServiceDll \ TargetService.obj 1

严重级代码描述项目文件行抑制状态 错误LNK2019未解析的外部符号__imp__UuidCreate @ 4在函数&#34中引用; long __cdecl _GenerateEndpointReference(wchar_t * *)&#34; (?_GenerateEndpointReference @@ YAJPAPA_W @ Z)ServiceDll C:\ test \ C ++ \ ServiceDll \ TargetService.obj 1

严重级代码描述项目文件行抑制状态 错误LNK1120 8未解析的外部服务ServiceDll C:\ test \ C ++ \ Debug \ ServiceDll.dll 1

1 个答案:

答案 0 :(得分:-1)

Ws2_32.lib;Wsdapi.lib;Rpcrt4.lib添加到您的链接库。