Call now: (800) 766-1884  



 Home


 SQL Server Tips
 SQL Server Training

 SQL Server Consulting
 SQL Server Support
 SQL Server Remote DBA



 Articles
 Services
 SQL Server Scripts
 Scripts Menu



 

 

 

 
 

SQL Server

SQL Server Tips by Gama and Naughter Consulting

 


Adding COM / Win32 DLL Support

We next need to implement the COM support. To do this, we create a COM object in the project, using the menu item “Insert -> New ATL Object”. We select the “Simple Object” type and specify a short name of “ServerReachable”. We accept all the other defaults values and click the “OK” button. In the “ClassView”, we select the “IServerReachable” COM interface in the class “CServerReachable” and using the context menu, select “Add Method”. We specify a method name of “ServerReachable” and specify parameters as “[in] BSTR Server, [out, retval] VARIANT_BOOL* pbReachable”. This completes the framework code for COM support.

We next need to add support for “IP Helper” to our project. We do this by adding the following to the bottom of our precompiled header “stdafx.h”:

#include <IPHlpApi.h>
#include <Icmpapi.h>

In addition we need to add “IPHlpApi.lib” and “wsock32.lib” to the “Object / Library Modules” setting in the Project settings linker tab. Make sure you update the settings for both your Release as well as Debug build configurations. In addition you might want to take this opportunity to remove unreferenced import libraries.

Now we implement the Win32 DLL support by creating a new “PingMachine.cpp / h” pair of source code modules. “PingMachine.h” contains the following:

#ifndef __PINGMACHINE_H__
#define __PINGMACHINE_H__

#ifdef BUILD_XP_SERVERREACHABLE
#define XP_SERVERREACHABLE_API __declspec(dllexport)
#else
#define XP_SERVERREACHABLE_API __declspec(dllimport)
#endif

//This function forms the Win32 DLL implementation
extern "C" BOOL XP_SERVERREACHABLE_API IcmpServerReachable(LPCTSTR pszServer);

#endif //__PINGMACHINE_H__

and “PingMachine.cpp” contains the following code:

#include "stdafx.h"
#include "PingMachine.h"

//This function forms the Win32 DLL implementation
BOOL IcmpServerReachable(LPCTSTR pszServer)
{
//Create the ICMP handle
HANDLE hICMP = IcmpCreateFile();
if (hICMP == INVALID_HANDLE_VALUE)
return FALSE;

//Do a DNS lookup of the server name if not already in dotted notation
LPSTR lpszAsciiServer = T2A((LPTSTR) pszServer);
unsigned long ipAddress = inet_addr(lpszAsciiServer);
if (ipAddress == INADDR_NONE)
{
//Not a dotted address, then do a lookup of the name
hostent* hp = gethostbyname(lpszAsciiServer);
if (hp)
memcpy(&ipAddress, hp->h_addr, hp->h_length);
else
return FALSE;
}


//assume the worst
BOOL bSuccess = FALSE;


//do the actual ping, note we hard code a number of values to reasonable default values

BYTE byDataToSend[32];
int nDataSize = sizeof(byDataToSend);
memset(byDataToSend, nDataSize, 'E'); //Use 32 "E"'s which is what the command line ping utility uses by default

//Set the options to use for the ping
IP_OPTION_INFORMATION ipOptions;
memset(&ipOptions, 0, sizeof(ipOptions));
ipOptions.Ttl = 10;

//Setup a stack buffer to receive the reply
BYTE byReplyBuffer[3];
int nReplySize = sizeof(byReplyBuffer);
DWORD nReplies = IcmpSendEcho(hICMP, ipAddress, byDataToSend, nDataSize, &ipOptions, byReplyBuffer, nReplySize, 5000); //Use a default timeout of 5 seconds

//Check the reply if one was received
if (nReplies == 1)
{
//Check what we got back is what we sent
bSuccess = TRUE;
ICMP_ECHO_REPLY* pEchoReply = (ICMP_ECHO_REPLY*) byReplyBuffer;
char* pReplyData = (char*) pEchoReply->Data;
for (int i=0; i<pEchoReply->DataSize && bSuccess; i++)
bSuccess = pReplyData[i] == 'E';
}

//Free the ICMP handle we have been using
IcmpCloseHandle(hICMP);

//Return the success indicator
return bSuccess;
}

At this point we also need to add a new pre-processor define “BUILD_XP_SERVERREACHABLE” to the C++ tab of the project’s settings. This ensures that the function is exported from the DLL when the project is built and the function is imported otherwise. Again make sure you add this define for all your project definitions. This completes the Win32 DLL implementation for the project.

We can now complete the COM support by simply calling the “IcmpServerReachable” function in the “ServerReachable” COM method as follows:

STDMETHODIMP CServerReachable::ServerReachable(BSTR Server, VARIANT_BOOL *pbReachable)
{
USES_CONVERSION;

//Validate our parameters
if (pbReachable == NULL)
return E_POINTER;

//Convert the BSTR string to a LPCTSTR
LPCTSTR pszServer = W2T(Server);

//Call the function we have developed
BOOL bReachable = IcmpServerReachable(pszServer);
if (bReachable)
*pbReachable = VARIANT_TRUE;
else
*pbReachable = VARIANT_FALSE;

return S_OK;
}

This completes the Win32 DLL and COM support.


The above book excerpt is from:

Super SQL Server Systems
Turbocharge Database Performance with C++ External Procedures

ISBN: 0-9761573-2-2
Joseph Gama, P. J. Naughter

 http://www.rampant-books.com/book_2005_2_sql_server_external_procedures.htm

 


 

 

 

Burleson Consulting Remote DB Administration


 

 


 

 

 

 

 
Burleson is the America's Team

Note: The pages on this site were created as a support and training reference for use by our staff of DBA consultants.  If you find it confusing, please exit this page.

Errata?  SQL Server technology is changing and we strive to update our SQL Server support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:and include the URL for the page.
 


Burleson Consulting
SQL Server database support

 

Copyright © 1996 -  2013 by Vaaltech Web Services. All rights reserved.

Hit Counter