Search code examples
c++sql-serversql-server-2008extended-procedures

MSSQL could not find the function in the library


I would like to test extended stored procedure (I know that they are now deprecated, but from personal reason I would like to test them).

I have generate a dll file in VC++ and here is my code:

//First.h
#include<iostream>
#include<srv.h>

using namespace std;

RETCODE _declspec(dllexport)  xp_firstfun(SRV_PROC *srvproc);

and

//main.cpp
#include<iostream>
#include "First.h"

using namespace std;


RETCODE xp_firstfun(SRV_PROC *srvproc)
{
    cout<<"Hello World froom DLL"<< endl;
    cin.get();
    return 0;
}

I can successfully add this dll to database with this command:

sp_addextendedproc 'xp_firstfun', 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\FirstDLL.dll'

but if I try to execute the function:

exec xp_firstfun

I am facing to this error:

Could not find the function xp_firstfun in the library C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn\FirstDLL. Reason: 127(procedure is not found).

I have two questions:

  • Is my C++ code correct?
  • Should I do something more in SQL to call this function in dll?

thank you for your helps


Solution

  • change your declaration. This may help you as in c++ the function name gets modified during compilation as part of name mangling.

    extern "C"
    {
        RETCODE _declspec(dllexport)  xp_firstfun(SRV_PROC *srvproc);
    }