none
windows 2008 x64 使用RAS Administration DLL RRS feed

  • 问题

  • 我找到了一个RAS Administration DLL 的例子,默认编译是x86的dll,在win2k3上使用正常,放到2k8上报告不是有效的 win32 应用程序。
    把工程改为x64模式编译会报错
    代码如下:
    UserRestrict.h
    #ifndef _UserRestrict_H_
    #define _UserRestrict_H_
    
    #define _WIN32_WINNT 0x500
    
    #include <windows.h>
    #include <sddl.h>
    #include <winsock.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <mprapi.h>
    #include <raserror.h>
    #include <mprerror.h>
    #include <strsafe.h>
    
    // DEFINES
    
    #define LOG_FILE	L"log.txt"
    #define MAX_IPV6_STRING 46
    #define MAX_SID_SIZE 256
    #define IPV6_INTERFACE_ID_LENGTH_IN_BYTES 8
    #define ADMINDLL_KEY	L"SOFTWARE\\Microsoft\\Ras\\AdminDll"
    #define DISPLAYNAME_VALUE	L"DisplayName"
    // PROTOTYPES
    
    DWORD InitializeDll();
    void CleanUp();
     void ConvertNameToSid(WCHAR NameString[],WCHAR **ReturnSid);
    
    struct usertable //datastructure to store the usernames
    {
    	WCHAR Username[UNLEN+2];
    	struct usertable *next;
    };
    
    
    #endif
    
    
     
    UserRestrict.c
    #ifndef UNICODE
    #define UNICODE
    #endif
    
    
    #include "UserRestrict.h" 
    
     // The log file to which we will log the information 
     // when each callback function is called
    
     
    FILE			*g_fpFile	= NULL; 
    
    struct usertable * head= NULL;
    
    
    //
    // Function is called whenever a link is disconnected. 
    //
    // Parameters: pointer to a RAS_PORT_0 structure
    //	   pointer to a RAS_PORT_1 structure
    //
    VOID WINAPI 
    MprAdminLinkHangupNotification(RAS_PORT_0 * pRasPort0,
                    RAS_PORT_1 * pRasPort1)
    {
    	fwprintf(g_fpFile,L"============MprAdminLinkHangupNotification callback============\n");
    	fflush(g_fpFile);
    	return;
    }     
    
    //
    // Function is called whenever a call is disconnected. 
    //
    // Parameters: pointer to a RAS_CONNECTION_0 structure
    //	   pointer to a RAS_CONNECTION_1 structure
    //	   pointer to a RAS_CONNECTION_2 structure
    //
    VOID WINAPI 
    MprAdminConnectionHangupNotification2(RAS_CONNECTION_0 * pRasConnection0,
                       RAS_CONNECTION_1 * pRasConnection1,
                       RAS_CONNECTION_2 * pRasConnection2)
    {
      
    	struct usertable *temp=NULL;
    	struct usertable *temp1=NULL;
    	struct usertable *newnode=NULL;
    
    	WCHAR *Sid;
    	
    	
    	fwprintf(g_fpFile,L"============MprAdminConnectionHangupNotification3 callback============\n");
    	
    	fwprintf(g_fpFile,L"Username: %s\n",pRasConnection2->wszUserName);
    
    	Sid = (WCHAR *)malloc(MAX_SID_SIZE*sizeof(WCHAR));
    
    	ConvertNameToSid((pRasConnection2->wszUserName),&Sid);
    
    	fwprintf(g_fpFile,L"Sid for user: %s\n",Sid);
    
    
    	if (!_wcsicmp(head->Username,Sid))
    	{
    		
    
    		temp = head->next;
    		
    		head-> next = NULL;
    		free(head);
    
    		head = temp;
    		fwprintf(g_fpFile,L"Removing user from list\n");
    		
    		fflush(g_fpFile);
    		return;
    	}
    
    	temp = head;
    	temp1 = temp->next;
    
    	fwprintf(g_fpFile,L"Removing user from list\n");
    	fflush(g_fpFile);
    	
    	while (temp1->next!= NULL)
    	{
    		
    		if (!_wcsicmp(temp1->Username,Sid))
    		{
    			temp->next=temp1->next;
    
    			temp1->next = NULL;
    			return;
    		}
    	
    		temp = temp-> next;
    		temp1 = temp1->next;
    	}
      if (temp1->next == NULL)
    	{
        
    		if (!_wcsicmp(temp1->Username,Sid))
    		{
    			temp->next=temp1->next;
    
    			temp1->next = NULL;
    		}
    	}
     	
      
      return;
    }     
    
    
    
    //
    // Function is called whenever a call is connected.
    //
    // Parameters: pointer to a RAS_CONNECTION_0 structure
    //			  pointer to a RAS_CONNECTION_1 structure
    //			  pointer to a RAS_CONNECTION_2 structure
    //
    BOOL WINAPI 
    MprAdminAcceptNewConnection2(RAS_CONNECTION_0 * pRasConnection0,
                   RAS_CONNECTION_1 * pRasConnection1,
                   RAS_CONNECTION_2 * pRasConnection2)
    {
      
    	struct usertable *temp = NULL;
    	struct usertable *newnode = NULL;
    	int flag = 0;
    
    	WCHAR *Sid;
    	
    	
    	fwprintf(g_fpFile,L"============MprAdminAcceptNewConnection3 callback============\n");
    	
    	
    	fwprintf(g_fpFile,L"Username: %s\n",pRasConnection2->wszUserName);
    
    
    	ConvertNameToSid((pRasConnection2->wszUserName),&Sid);
    
    	fwprintf(g_fpFile,L"Sid for user: %s\n",Sid);
    	
    
    	if (!head) //first time
    	{
    		head = (struct usertable *) malloc(sizeof(struct usertable));
    		if (head == NULL)
    			{
    			  fwprintf(g_fpFile,L"Not enough memory!\n");
      			  fflush(g_fpFile);
    			  return FALSE;
    			}
    		 else
    		 	{
    				
    				StringCchCopy(head ->Username,UNLEN+2,Sid); 
    				head->next =NULL;
    				fwprintf(g_fpFile,L"connection accepted\n");
    		 	}
    		
    		fflush(g_fpFile);
    		return TRUE;
    	}
      
    	temp = head;
    
    	while (temp->next!= NULL)
    	{
    
    		if (!_wcsicmp(temp->Username,Sid))
    		{
    			flag =1;
    			
    		}
    		temp = temp-> next;
    	}
      if (!_wcsicmp(temp->Username,Sid)) //check last node
    	{
    
    		flag =1;
    	}
    
    		
    	if (flag !=1) //User not present, so add user to list
    	{
    			newnode = (struct usertable *) malloc(sizeof(struct usertable));
    			if (newnode!=NULL)
    			{
    				StringCchCopy(newnode ->Username,UNLEN+2,Sid); 
    				newnode->next =NULL;
    				temp->next = newnode;
    
    				fwprintf(g_fpFile,L"connection accepted\n");
    				fflush(g_fpFile);
    			}
    			else
    			{
    			    fwprintf(g_fpFile,L"Memory allocation failed!\n");
    				fflush(g_fpFile);
    				return FALSE;
    			}
    				
    				
    			
    	}
        else if (flag==1)
    	{
    	 	    fwprintf(g_fpFile,L"User already connected to server - so connection rejected\n");
    			 
    			fflush(g_fpFile);
    			return FALSE;
    	}
    	return TRUE;	
    }
             
    
    //
    // Function is called whenever a new link is made.
    //
    // Parameters: pointer to a RAS_PORT_0 structure
    //			  pointer to a RAS_PORT_1 structure
    //
    BOOL WINAPI 
    MprAdminAcceptNewLink(RAS_PORT_0 * pRasPort0,
               RAS_PORT_1 * pRasPort1)
    {
    	  fwprintf(g_fpFile,L"============MprAdminAcceptNewLink callback============\n");
    
    	  // We log the port to which the user connected
    	  fwprintf(g_fpFile,L"Link accepted on port : %s\n",pRasPort0->wszPortName);
    	  fflush(g_fpFile);	  
          	  return TRUE;
      }
    
    
        
    //
    // Function used for DLL specific initialization. 
    //
    // Returns: NO_ERROR if successful
    //		  The last error number if an error is encountered
    //
    DWORD WINAPI 
    MprAdminInitializeDll(void)
    {
      DWORD dwRetVal = ERROR_SUCCESS;
      dwRetVal = InitializeDll();
      if ( ERROR_SUCCESS != dwRetVal ) 
        fwprintf(g_fpFile, L"Initialization function failed with error %d\n", dwRetVal);
      else  
        fwprintf(g_fpFile,L"RAS Admin DLL initialized.\n");
    
      fflush(g_fpFile);
      return dwRetVal;
    }
    
    //
    // Function used to form DLL specific cleanup. 
    //
    DWORD WINAPI 
    MprAdminTerminateDll(void)
    {
      	
    	
    	CleanUp();
      	return NO_ERROR;
    }
    
    //
    // Function initializes everything needed 
    //
    // Returns: TRUE if all is well
    //			FALSE otherwise
    DWORD
    InitializeDll()
    {
    	HKEY	hKey = NULL;
    	WCHAR	DLLName[MAX_PATH];
    	DWORD dwBufferLength = MAX_PATH;
    	DWORD dwRetval = ERROR_SUCCESS;
    	
    	fwprintf(g_fpFile,L"Terminating RAS Admin DLL ...\n");
    	//
    	// Open the log file. 
    	//
    	if ((dwRetval = _wfopen_s(&g_fpFile,LOG_FILE,L"w")) != 0)
    	{
    		goto CleanUp;
    	}
    
      
    	dwRetval = RegOpenKeyEx(HKEY_LOCAL_MACHINE,ADMINDLL_KEY,0,KEY_QUERY_VALUE,&hKey);
    	if ( ERROR_SUCCESS != dwRetval )
    	{
    	  fwprintf(g_fpFile,L"Could not open registry key required for Admin DLL!\n");
    		goto CleanUp;
    	}
    
    	dwRetval = RegQueryValueEx(hKey,DISPLAYNAME_VALUE,NULL,NULL,(LPBYTE)DLLName,&dwBufferLength);
    	if ( ERROR_SUCCESS != dwRetval)
      {
        fwprintf(g_fpFile,L"Display Name of Admin DLL not found in registry!\n");
    		goto CleanUp;
      }
    
     CleanUp:
    	if (hKey)
    		RegCloseKey(hKey);
    
      if (dwRetval== ERROR_SUCCESS)
    	{
    		fwprintf(g_fpFile,L"Display Name of Admin DLL: %s\n",DLLName);
    		fflush(g_fpFile);
    	}
    	else
    	{
    		fflush(g_fpFile);
    		CleanUp();
    	}    
    	
    	return dwRetval;
    }     
    
    
     void ConvertNameToSid(WCHAR NameString[],WCHAR **ReturnSid)
     {
      // Converts the username to Sid to store in the link list
      BOOL bRet = FALSE;
      DWORD dwSidSize = 0;
      DWORD dwReferenceSize = 0;
      SID_NAME_USE SidNameUSe;
      BYTE* pSid;
      //WCHAR *StringSid;
      DWORD dwErr = ERROR_SUCCESS;
      WCHAR *ReferenceString;
      
      bRet = LookupAccountName(NULL,
      	                   NameString,
      	                   NULL,
      	                   &dwSidSize,
      	                   NULL,
      	                   &dwReferenceSize,
      	                   &SidNameUSe);
    
     fwprintf(g_fpFile,L"dwSidSize = %d\n",dwSidSize);
     
      	 pSid = (BYTE*)malloc(dwSidSize*sizeof(BYTE));
    	 if (pSid ==NULL)
    	 	{
    	 	 fwprintf(g_fpFile,L"Could not allocate memory!\n");
    		 goto CleanUp;
    	 	}
         ReferenceString = (WCHAR*)malloc(dwReferenceSize*sizeof(WCHAR));
    	 if (ReferenceString ==NULL)
    	 	{
    	 	 fwprintf(g_fpFile,L"Could not allocate memory!\n");
    		 dwErr = ERROR_NOT_ENOUGH_MEMORY;
    		 goto CleanUp;
    	 	}	 
    	 bRet = LookupAccountName(NULL,
      	                   NameString,
      	                   pSid,
      	                   &dwSidSize,
      	                   ReferenceString,
      	                   &dwReferenceSize,
      	                   &SidNameUSe);
    
    	 if (bRet != TRUE)
    	 	{
    	 	 fwprintf(g_fpFile,L"LookupAccountName failed!!\n");
    		 dwErr = GetLastError();
    		 fwprintf(g_fpFile,L"Error is %d\n",dwErr);
    		 goto CleanUp;
    	 	}
    
    	 if(!IsValidSid(pSid)) 
    	 	{
            fwprintf(g_fpFile,L"Invalid SID!!\n");
    		 dwErr = ERROR_INVALID_FUNCTION;
    		 goto CleanUp;
    	 	}
    
    	 bRet = ConvertSidToStringSid(pSid,ReturnSid);
    
    	 if (bRet != TRUE)
    	 	{
    	 	 fwprintf(g_fpFile,L"ConvertSidToStringSid failed!!\n");
    		 dwErr = ERROR_INVALID_FUNCTION;
    		 goto CleanUp;
    	 	}	 	
    
        //*ReturnSid = StringSid;
    
     
     
      CleanUp:
      	if (ReferenceString !=NULL) 
      		 free(ReferenceString);
    
    	 if (pSid !=NULL)
    	    free(pSid);
    		
      	return;
    
    }
    
    //
    // Function cleans up all threads, handles, memory, etc.
    //
    void
    CleanUp()
    {
    	
    	//
    	// Close the log file
    	//
    	_fcloseall();
    }
    
    
    
     
    UserRestrict.def
    LIBRARY "RestrictOneConnPerUser.dll"
    
    EXPORTS
        MprAdminLinkHangupNotification	@1
        MprAdminAcceptNewConnection2 @2
        MprAdminConnectionHangupNotification2	@3
        MprAdminAcceptNewLink	@4
        MprAdminInitializeDll	@5
        MprAdminTerminateDll	@6
    
    
    
    我是搞c#的, 对c++ 不太熟悉,老解决不了问题,请高手帮忙解决。谢谢。
     
    2011年4月28日 15:33

答案

  • 我现在编译成x64 和x86 都可以了,引用的lib不一样,但是,部署到windows 2008 r2 服务器,会报告

     

    无法加载第三厂商的 RAS 管理 DLL 组件。发生下列错误: 找不到指定的模块。

     

    我在windows 2003 32位上 测试没问题,

    2011年4月29日 7:37
  • Hi max

    >>无法加载第三厂商的 RAS 管理 DLL 组件。发生下列错误: 找不到指定的模块。

    这很可能是服务器的问题。您的服务器没安装第三方组件。

     

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年5月3日 6:55
    版主

全部回复

  • 你在2008下用32位方式编译一下应该就可以了
    0xBAADF00D
    2011年4月29日 6:51
    版主
  • 我现在编译成x64 和x86 都可以了,引用的lib不一样,但是,部署到windows 2008 r2 服务器,会报告

     

    无法加载第三厂商的 RAS 管理 DLL 组件。发生下列错误: 找不到指定的模块。

     

    我在windows 2003 32位上 测试没问题,

    2011年4月29日 7:37
  • Hi max

    >>无法加载第三厂商的 RAS 管理 DLL 组件。发生下列错误: 找不到指定的模块。

    这很可能是服务器的问题。您的服务器没安装第三方组件。

     

    Lucy


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年5月3日 6:55
    版主