Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my C++ extended stored procedure not write to the console?

I am trying to write simple extended stored procedure using C++ dll for writing some text to console. I have made it in next way:

  1. created a dll using c++ (look at .cpp and .h files below)
  2. tested this dll from win32 console app (all worked as tic tak toe)
  3. move got dll file into sql server bin directory
  4. executed sp_addextendedproc 'xp_test','xp_test.dll' from management studio
  5. executed extended proc(exec xp_test)

When I have executed the last step there is a prompt appeared that notify me that query is executed saccesfully but there is no any console window with expected hello world prompt. Where is my fault?

//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;
}

I also have tried to use Console API but this also do not allow to write to console

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;
}
like image 850
user2201747 Avatar asked Nov 29 '25 09:11

user2201747


1 Answers

Although I don't know anything about C++, there is no console because SQL Server is usually running as a service without an interactive session or desktop, so it isn't clear where you expect the output to go.

As the documentation explains, an extended stored procedure returns result sets to the server using the Extended Stored Procedure API, but your code doesn't seem to do this.

Fortunately, Microsoft actually provides a Hello World example of their own; have you reviewed it?

And finally, please note the warnings in MSDN that extended stored procedures are now deprecated, will be removed from SQL Server and should not be used for new development.

like image 146
Pondlife Avatar answered Dec 01 '25 22:12

Pondlife



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!