[英]Why does my C++ extended stored procedure not write to the console?
我正在尝试使用C ++ DLL编写简单的扩展存储过程,以便将一些文本写入控制台。 我已经用下一个方式做到了:
sp_addextendedproc 'xp_test','xp_test.dll'
exec xp_test
) 当我执行了最后一步时,会出现一个提示,通知我查询是在执行时执行的,但是没有任何控制台窗口有预期的hello world提示。 我的错在哪里?
//test.h file
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" __declspec(dllimport)
#endif
#include <srv.h>
MYLIBAPI SRVRETCODE xp_test(SRV_PROC *srvproc);
// xp_test.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#define MYLIBAPI extern "C" __declspec(dllexport)
#include "test.h"
#include <iostream>
using namespace std;
SRVRETCODE xp_test(SRV_PROC *srvproc){
cout << "Hello world" << endl;
cin.get();
return 0;
}
我也尝试使用Console API,但这也不允许写入控制台
SRVRETCODE xp_test(SRV_PROC *srvproc){
AllocConsole();
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD writtenCount;
LPCTSTR msg = L"Hello world\n";
WriteConsole(out, msg, lstrlen(msg), &writtenCount, NULL);
Sleep(5000);
return 0;
}
虽然我对C ++一无所知,但没有控制台,因为SQL Server通常作为没有交互式会话或桌面的服务运行,因此不清楚您希望输出的位置。
由于文档说明 ,扩展存储过程返回结果集使用服务器的扩展存储过程API ,但你的代码似乎并没有做到这一点。
幸运的是,微软实际上提供了他们自己的Hello World示例 ; 你看过吗?
最后,请注意MSDN中的警告 ,即扩展存储过程现已弃用,将从SQL Server中删除,不应用于新开发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.