简体   繁体   English

C++ 用 Visual Studio 编译 windows 服务

[英]C++ Compiling windows service with visual studio

hope you could help me.希望你能帮助我。 I got a C++ source for my service based on Microsoft example , still i get linker error compiling it:我得到了一个基于Microsoft 示例的服务的 C++ 源代码,但我仍然收到 linker 错误编译它:

error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

as the entry point for windows services is int _tmain(int argc, TCHAR* argv[])作为 windows 服务的入口点是int _tmain(int argc, TCHAR* argv[])

In my case it's void __cdecl _tmain(int argc, TCHAR* argv[]) {... }就我而言,它是void __cdecl _tmain(int argc, TCHAR* argv[]) {... }

There are 1 header and 1 cpp files with a class used by the service and main.cpp contaning entry point and c style service related code.有 1 个 header 和 1 个 cpp 文件,其中 class 由服务使用,main.cpp 包含入口点和 c 样式服务相关代码。 Subsystem is console without any custom entry point set.子系统是没有设置任何自定义入口点的控制台。 Still if i add classic int main(...) to the code project compiles yet the service does not start from windows service manager returning error.仍然,如果我将经典int main(...)添加到代码项目编译中,但服务不会从 windows 服务管理器返回错误开始。

Please advise how to compile this using _tmain .请告知如何使用_tmain编译它。

Ok, still no luck with _tmain .好的,仍然没有运气_tmain Unfortunately i haven't tried to define WINAPI WinMain entry point.不幸的是,我没有尝试定义WINAPI WinMain入口点。

Still if you proceed with wmain program entry point and add code mentioned by Richard Critten for a service startup SCManager performs just fine.尽管如此,如果您继续使用wmain程序入口点并添加Richard Critten 提到的用于服务启动的代码,SCManager 就可以正常运行。 For a complete service sample refer to MS doc .有关完整的服务示例,请参阅MS doc wmain will force you to use Unicode yet it shouldn't be such a problem in 2020. wmain会强迫你使用 Unicode 但它在 2020 年应该不是这样的问题。

Subsystem should be set as supposed to your needs and no need to set custom entry point.应根据您的需要设置子系统,无需设置自定义入口点。

Thanks to everyone commented.感谢大家的评论。

UPD You have to include tchar.h in order to use _tmain , so all types of entry points will work fine. UPD您必须包含tchar.h才能使用_tmain ,因此所有类型的入口点都可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM