20140110orange.cpp

5 篇文章 0 订阅
#define WINVER 0x0502 
// using compiler option /MD must define _AFXDLL
//#define _AFXDLL 
#include "afxinet.h" 
#include "lm.h" 

#include "strsafe.h" //StringCchPrintf

extern "C" { 
#include "lua.h" 
#include "lualib.h" 
#include "lauxlib.h" 
} 

#include <windows.h> 
#include <Iptypes.h> 
#include <iphlpapi.h> 
#include <winsock2.h> 

#include <iostream> 
using namespace std; 
/*Linker command line additional options
        /FORCE:Multiple 
*/ 
#pragma comment(lib,"netapi32.lib")
#pragma comment(lib,"version.lib")
#pragma comment(lib,"iphlpapi.lib")

//0:操作成功完成。 
//86:指定的网络密码不正确。 
//1327:登录失败: 用户帐户限制。可能的原因包括不允许空密码,登录时间限制,或强制的策略限制。
//1351:未能从域控制器读取配置信息,或者因为机器不可使用,或者是访问被拒绝。
//1909:引用的帐户当前已锁定,且可能无法登录。 
extern "C" int lua_chp(lua_State* L) {
        LPCWSTR host; 
        LPCWSTR user; 
        LPCWSTR pwd; 
        LPCWSTR npwd; 
        host=(LPCWSTR)luaL_optstring(L,1,"");
        user=(LPCWSTR)luaL_optstring(L,2,"");
        pwd=(LPCWSTR)luaL_optstring(L,3,"");
        npwd=(LPCWSTR)luaL_optstring(L,4,"");
        DWORD nstatus=NetUserChangePassword(host,user,pwd,npwd);
        int iret=1; 
        const char* errstr=0;
        if (nstatus) { 
                switch(nstatus) {
                case ERROR_ACCESS_DENIED: errstr="ERROR_ACCESS_DENIED"; break;
                case ERROR_INVALID_PASSWORD: errstr="ERROR_INVALID_PASSWORD"; break;
                case NERR_InvalidComputer: errstr="NERR_InvalidComputer"; break;
                case NERR_NotPrimary: errstr="NERR_NotPrimary"; break;
                case NERR_UserNotFound: errstr="NERR_UserNotFound"; break;
                case NERR_PasswordTooShort: errstr="NERR_PasswordTooShort"; break;
                case NERR_Success: errstr="NERR_Success"; break;
                default: errstr="unknow error";
                } 
                iret=2; 
                lua_pushnumber(L, nstatus);
                lua_pushstring(L, errstr);
        } else { iret=1; lua_pushnumber(L, nstatus); }
        return iret; 
} 

extern "C" int lua_geturl(lua_State* L) {
        CInternetSession Inets;
        const char* url=url=luaL_optstring(L,1,"");
        Inets.OpenURL(url); 
        return 0; 
} 

extern "C" int lua_getfi(lua_State* L) {
  int nret = 0; 
  const char* filename = lua_tostring(L, 1);
  const char* request = lua_tostring(L, 2);

  char strTmp[512]; 
  CString strRet; 
  DWORD handle = 0; //didn't actually use (dummy var)
  int infoSize = (int)GetFileVersionInfoSize(filename, &handle);
  if(infoSize == 0) { 
    lua_pushnil(L); 
    lua_pushstring(L, "Function <GetFileVersionInfoSize> unsuccessful!");
    nret+=2; goto LABEL_EXIT;
  } 

  LPVOID pBlock; 
  pBlock = new BYTE[infoSize];

  int bResult = GetFileVersionInfo(filename, handle, infoSize, pBlock);
  if(bResult == 0){ 
    lua_pushnil(L); 
    lua_pushstring(L, "Function <GetFileVersionInfo> unsuccessful!");
    nret+=2; goto LABEL_EXIT;
  } 

  // Structure used to store enumerated languages and code pages.
  struct LANGANDCODEPAGE { 
    WORD wLanguage; 
    WORD wCodePage; 
  } *lpTranslate; 

  LPVOID lpBuffer; 
  UINT dwBytes; 
  int i, langNumber; 

  // Read the list of languages and code pages.
  bResult = VerQueryValue(pBlock, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &dwBytes);
  if(bResult == 0){ 
    lua_pushnil(L); 
    lua_pushstring(L, "Function <VerQueryValue> for Translation unsuccessful!");
    nret+=2; goto LABEL_EXIT;
  } 
  
  //langNumber always must be equal 1 (in my program)
  langNumber = dwBytes/sizeof(struct LANGANDCODEPAGE);
  if(langNumber != 1){ 
    sprintf(strTmp, "Error! Languages number: %d.", langNumber);
    lua_pushnil(L); 
    lua_pushstring(L, strTmp);
    nret+=2; goto LABEL_EXIT;
  } 

  /* request maybe one of following value
    Comments 
    CompanyName 
    FileDescription 
    InternalName 
    LegalTradeMarks 
    LegalCopyright 
    OriginalFileName 
    ProductName 
    ProductVersion 
    PrivateBuild 
    SpecialBuild 
  */ 
  // Read the file description for each language and code page.
  HRESULT hr = 0; 
  for(i=0; i<langNumber; i++){
    hr = StringCchPrintf(strTmp, 512, TEXT("\\StringFileInfo\\%04x%04x\\%s"),
      lpTranslate[i].wLanguage, lpTranslate[i].wCodePage, request);

    if (FAILED(hr)){ 
      lua_pushnil(L); 
      lua_pushstring(L, "Unable to get information in this language!");
      nret+=2; goto LABEL_EXIT;
    } 

    // Retrieve file description for language and code page "i".
    bResult = VerQueryValue(pBlock, strTmp, &lpBuffer, &dwBytes);
    if(bResult == 0){ 
      lua_pushnil(L); 
      lua_pushstring(L, "Function <VerQueryValue> for StringFileInfo unsuccessful!");
      nret+=2; goto LABEL_EXIT;
    } 
  } 

  strRet = (char*)lpBuffer; 
  delete [] pBlock; 
  strRet.Replace(", ", "."); 
  lua_pushstring(L, strRet); 
  ++nret; 
  
LABEL_EXIT: 
  return nret; 
} 

int GetHostByName(const char* strHost, string& strIP) {
  WSADATA wsaData; 
  int iResult = 0; 

  DWORD dwError; 
        char temp[1024]; 

  // Initialize Winsock 
  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  if (iResult != 0) { 
    sprintf(temp, "WSAStartup failed: %d\n", iResult);
    strIP = temp; 
    iResult = 1; 
  } else { 
    hostent* remoteHost = gethostbyname(strHost);
    if (remoteHost == NULL) {
      dwError = WSAGetLastError();
      if (dwError != 0) { 
        if (dwError == WSAHOST_NOT_FOUND) {
          strIP = "Host not found";
        } else if (dwError == WSANO_DATA) {
          strIP = "No data record found";
        } else { 
          sprintf(temp, "Function failed with error: %ld\n", dwError);
          strIP = temp; 
        } 
                        } 
                        iResult = 1;
                } else { 
                        in_addr* address = (in_addr*)remoteHost->h_addr;
                        strIP = inet_ntoa(*address);
                } 
        } 
        // Clean up Winsock 
        WSACleanup(); 
        return iResult; 
} 

void GetIP(string &strResult) {
  IP_ADAPTER_INFO *pAI = (IP_ADAPTER_INFO*)malloc(sizeof(IP_ADAPTER_INFO));
  ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
  if (GetAdaptersInfo(pAI, &ulOutBufLen)==ERROR_BUFFER_OVERFLOW){
    free(pAI); 
    pAI=(IP_ADAPTER_INFO*)malloc(ulOutBufLen);
  } 
  DWORD dwRet = ::GetAdaptersInfo(pAI, &ulOutBufLen);
  if (ERROR_SUCCESS == dwRet){
    _IP_ADDR_STRING *p = &pAI->IpAddressList;
    //do { 
    strResult += p->IpAddress.String;
    //    strResult += " "; 
    //}while(p=p->Next); 
    //strResult = pAI->CurrentIpAddress->IpAddress.String;
  } 
  free(pAI); 
} 

void GetMAC(const string &ip, string &strResult) {
  IPAddr ipAddr; 
  ipAddr = inet_addr(ip.c_str());

  ULONG pulMac[2]; 
  ULONG ulLen = 6; 
  HRESULT hr = ::SendARP(ipAddr, 0, pulMac, &ulLen);
  if (NO_ERROR==hr){ 
    PBYTE pbHexMax = (PBYTE)pulMac;
    LONG i = ulLen; 
    strResult = ""; 
    char part[64]; 
    for (; i>0; --i){ 
      sprintf(part, "%02X", pbHexMax[ulLen - i]);
      strResult += part; 
      if (i > 1) strResult += "-";
    } 
  } 
} 

int lua_GetHostByName(lua_State* L) {
  const char* host = luaL_checkstring(L, 1);
  string strIP; 
  int result = 0; 
  if (NULL != host){ 
    if (GetHostByName(host, strIP)){
      lua_pushnil(L); 
      lua_pushstring(L, strIP.c_str());
      result = 2; 
    } else { 
      lua_pushstring(L, strIP.c_str());
      result = 1; 
    } 
  } 
  return result; 
} 

int lua_GetIPConfig(lua_State* L) {
  string strIP; 
  string strMAC; 
  GetIP(strIP); 
  GetMAC(strIP, strMAC); 

  lua_pushstring(L, strIP.c_str());
  lua_pushstring(L, strMAC.c_str());

  return 2; 
} 

extern "C" int lua_help(lua_State* L) {
        const char* s= 
                "orange module \n"
                "  chp :    NetUserChangePassword(host,user,pwd,npwd)\n"
                "  geturl:  CInternetSession.OpenURL(url)\n"
    "  getfi:   GetFileInformation\n"
    "  getip:   return local IP and MAC\n"
    "  gethost: return IP according to host name\n"
                "  help :   show this\n"
                "wunoman@qq.com 2013/12/09\n";
        lua_pushstring(L, s);
        return 1; 
} 

static const char* LUA_MODULE_NAME = "orange";
luaL_reg lrg_orange[] = { 
        {"chp", lua_chp}, 
        {"geturl", lua_geturl},
  {"getfi", lua_getfi}, 
        {"getip", lua_GetIPConfig},
        {"gethost", lua_GetHostByName},
        {"help", lua_help}, 
        {NULL,NULL} 
}; 

extern "C" int luaopen_orange(lua_State* L) {
        luaL_register(L, LUA_MODULE_NAME, lrg_orange);
        return 1; 
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值