Extended Stored Procedures from C++ Require extern "C"ID: Q205415
|
In order to compile C+/+ projects into a SQL Server extended procedure it is required to make the header file Srv.h externally declared as it was intended for ANSII "C" applications. Not doing this will result in the error "LNK2001" during compile time.
Mark the include file Srv.h as external. This is done with the C++ declarator "extern" and specifying the language as being "C". In the previous sample remove the comments on the lines:
//extern "C"{
#include <srv.h>
//}
To read:
extern "C"{
#include <srv.h>
}
The project should now compile with out any errors. To run the extended stored procedure, follow these steps:
"c:\simpXP\debug\simpXP.dll"
NOTE: The whole path and name must conform to the 8.3 naming conventions for SQL Server.
--**********************************************
sp_dropextendedproc xp_Easy
GO
sp_addextendedproc xp_Easy, "c:\simpXP\debug\simpXP.dll"
GO
Execute xp_Easy
--**********************************************
The output should look like:
"This is how easy XP's can be!"
//************************************************
#define DBNTWIN32
#include <windows.h>
#include <sqlfront.h>
#include <sqldb.h>
//extern "C"{
#include <srv.h>
//}
#define XP_NOERROR 0
#define SRV_MAXERROR 20000
#define CMDSHELL_ERROR SRV_MAXERROR + 2
int xp_Easy(SRV_PROC * srvproc)
{
srv_sendmsg(srvproc, SRV_MSG_INFO , CMDSHELL_ERROR, SRV_INFO, (DBTINYINT)0,
NULL, 0, 0, "This is how easy XP's can be!", SRV_NULLTERM);
srv_senddone(srvproc, SRV_DONE_MORE, 0, 0);
return(XP_NOERROR);
}//end
//************************************************
LIBRARY xp_Easy
DESCRIPTION 'SQL Server Extended Stored Procedure - DLL'
EXPORTS
xp_Easy
simpXP.obj : error LNK2001: unresolved external symbol "int __cdecl srv_senddone(struct srv_proc *,unsigned short,unsigned short,long)" (?srv_senddone@@YAHPAUsrv_proc@@GGJ@Z)
simpXP.obj : error LNK2001: unresolved external symbol "int __cdecl srv_sendmsg(struct srv_proc *,int,long,unsigned char,unsigned char,char *,int,unsigned short,char *,int)" (?srv_sendmsg@@YAHPAUsrv_proc@@HJEEPADHG1H@Z)
NOTE: If you get more (or different) errors it is likely Visual C++ does not know where the SQL Include or Library files are located. The easiest solution is to move the files to the \VC\INCLUDE directory from the PTK\ODS\INCLUDE directory and the file Ntwdblib.lib from \PTK\DBLIB\LIB to the \VC\LIB and the file Opends60.lib from \PTK\ODS\LIB to \VC\LIB.c:\simpXP3.dll : fatal error LNK1120: 2 unresolved externals
For more indepth examples of extended stored procedures please refer to the Programmer's Toolkit samples:
Q96433 INF: Using DB-Library with C++
Q157919 INF: Building an ODS Application with MFC
Q108522 FIX: L2029 Error Calling ODBC Functions from ODBC SDK
Additional query words: hello world simple
Keywords : SSrvESQL_C SSrvStProc SSrvTran_SQL kbSQLServ650 kbSQLServ700
Version : winnt:6.5,7.0
Platform : winnt
Issue type : kbhowto
Last Reviewed: June 2, 1999