标签:
// AddFavorate.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include "atlbase.h" #include <iostream> #include <fstream> #include <string> #include "stdlib.h" #include "stdio.h" #include "io.h" extern "C" { #include "lua.h" #include "lualib.h" #include "lauxlib.h" } #if defined(_WIN32) extern "C" __declspec(dllexport) int luaopen_MyLuaDLL(lua_State* L); #else extern "C" int luaopen_MyLuaDLL(lua_State* L); #endif #pragma comment(lib,"lualib.lib") lua_State * L; using namespace std; char* UnicodeToAnsi(TCHAR* wstr) //转换字符编码 { if( !wstr ) return NULL; int strlen = ::WideCharToMultiByte(CP_ACP, NULL, wstr, wcslen(wstr), NULL, 0, NULL, FALSE); char* str = new char[strlen + 1]; ::WideCharToMultiByte(CP_ACP, NULL, wstr, wcslen(wstr), str, strlen, NULL, FALSE); str[strlen] = ‘\0‘; return str; } char* read_reg_sz()//读取操作表,其类型为REG_SZ { HKEY hkey; LPCTSTR data_set = _T("\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"); //\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Favorites"‘ if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER, data_set, 0, KEY_READ, &hkey)) { TCHAR dwValue[256]; DWORD dwSzType = REG_SZ; DWORD dwSize = sizeof(dwValue); _tsetlocale(LC_ALL, _T("chs")); if (::RegQueryValueEx(hkey, _T("Favorites"), 0, &dwSzType, (LPBYTE)&dwValue, &dwSize) != ERROR_SUCCESS) { cout << "无法查询有关的注册表信息" << endl; } char* reValue; reValue = UnicodeToAnsi(dwValue); ::RegCloseKey(hkey); return reValue; } ::RegCloseKey(hkey); } int WriteFile(char* ljx,char *url,char* icon,char* name) //写文件操作; { FILE *stream; char lj[128]={}; char l1[256] = "[{000214A0-0000-0000-C000-000000000046}]\r\nProp3=19,11\r\n[InternetShortcut]\r\nIDList=\r\nURL="; char l3[256] = "\r\nIconFile="; char l5[] = "\r\nIconIndex=1\r\n"; strcat(lj,ljx); strcat(lj,"\\"); strcat(lj,name); if ((stream = fopen(lj, "wb")) == NULL) /* open file TEST.$$$ */ { fprintf(stderr, "Cannot open output file.\n"); return -1; } strcat(l1,url); strcat(l3,icon); fwrite(l1, sizeof(l1), 1, stream); fwrite(l3, sizeof(l3), 1, stream); fwrite(l5, sizeof(l5), 1, stream); fclose(stream); /*关闭文件*/ return 0; } LUALIB_API int AddFavorateIE(lua_State* L) //添加 IE 主函数; { string url; string icon; string name; int res = 0; //拾取栈 if( lua_isstring(L,-1)){ name = lua_tostring(L,-1); } if( lua_isstring(L,-2)){ icon = lua_tostring(L,-2); } if( lua_isstring(L,-3)){ url = lua_tostring(L,-3); } char* lj = read_reg_sz(); if (strlen((char*)url.c_str()) && strlen((char*)name.c_str()) && strlen((char*)icon.c_str())) { res = WriteFile(lj,(char*)url.c_str(),(char*)icon.c_str(),(char*)name.c_str()); } else { res = -1; lua_pushnumber(L,res); return 1; } lua_pushnumber(L,res); return 1; } int AddPage(char* url,char* name) // 写书签 { //Win7 及以上目录; char Lj[128] = "C:"; strcat(Lj,getenv("homepath")); char i[]= "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks"; char j[]= "\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks1"; char Lj1[128]={0}; strcpy(Lj1,Lj); strcat(Lj,i); strcat(Lj1,j); // XP 兼容目录; FILE *fp; if((fp=fopen(Lj,"r"))==NULL) { char Ljx[128] = "C:"; strcat(Ljx,getenv("homepath")); char ix[]= "\\Local Settings\\Application Data\\Google\\Chrome\\User Data\\Default\\Bookmarks"; char jx[]= "\\Local Settings\\Application Data\\Google\\Chrome\\User Data\\Default\\Bookmarks1"; char Lj1x[128]={0}; strcpy(Lj1x,Ljx); strcat(Ljx,ix); strcat(Lj1x,jx); if((fp=fopen(Ljx,"r"))==NULL) { return -1; } strcpy(Ljx,Lj); strcpy(Lj1x,Lj1); } fclose(fp); /////////////////////////////////////////////////////////// ifstream in(Lj); ofstream out(Lj1); string filename; string line; string str="children"; string str1 = "],"; bool tof = true; //创建拷贝文件; while (getline (in, line)) { if (tof && strstr(line.c_str(),str.c_str())) { if (strstr(line.c_str(),str1.c_str())) { out << " \"children\": [ " << endl; out<<" \"date_added\": \"13087947012000000\","<<endl; out << " \"id\": \"2\"," << endl; out << " \"name\": \""<<name<<"\"," << endl; out << " \"type\": \"url\"," << endl; out << " \"url\": \""<<url<<"\"" << endl; out << " } ]," << endl; } else { out << line << endl; out<<" \"date_added\": \"13087947012000000\","<<endl; out << " \"id\": \"2\"," << endl; out << " \"name\": \""<<name<<"\"," << endl; out << " \"type\": \"url\"," << endl; out << " \"url\": \""<<url<<"\"" << endl; out << " }, {" << endl; } tof = false; } else { out << line << endl; } } in.close(); out.close(); //删除重命名; remove(Lj); rename(Lj1,Lj); return 0 ; } LUALIB_API int AddFavorateCh(lua_State* L) { string url; string icon; string name; int res = 0; //拾取栈 if( lua_isstring(L,-1)){ name = lua_tostring(L,-1); } if( lua_isstring(L,-2)){ url = lua_tostring(L,-2); } char* lj = read_reg_sz(); if (strlen((char*)url.c_str()) && strlen((char*)name.c_str())) { res = AddPage((char*)url.c_str(),(char*)name.c_str()); } else { res = -1; lua_pushnumber(L,res); return 1; } lua_pushnumber(L,res); return 1; } static const struct luaL_reg MyLuaDLLFunctions[]= { {"AddFavorateIE",AddFavorateIE}, {"AddFavorateCh",AddFavorateCh}, {NULL, NULL} }; int __declspec(dllexport) luaopen_MyLuaDLL(lua_State* L) { luaL_openlib(L, "MyLuaDLLFunctions", MyLuaDLLFunctions, 0); return 0; }
以上Dll代码
function main() --入口 local testlib = package.loadlib("C:\\Users\\丁宁\\Documents\\Visual Studio 2010\\Projects\\AddFavorate\\Debug\\AddFavorate.dll", "luaopen_MyLuaDLL") print(testlib)--, "Can not open testlib.") testlib() --A = MyLuaDLLFunctions.AddFavorateIE("http://www.baidu.com/","https://www.baidu.com/favicon.ico/","baxxxxxxxxxxxxxxxxxxxxxxxxxxxxidu.url") A = MyLuaDLLFunctions.AddFavorateCh("http://www.baidu.com/","baidu") print(A) end
Lua测试代码
void tiaoshi() { cout<<"调取LUA中函数"<<endl;//LUA入口 lua_getglobal(L,"main"); if(!lua_isfunction(L,-1)) { const char* error = lua_tostring(L,-1); cout<<string(error)<<endl; } if(lua_pcall(L,0,0,0)) { const char* error = lua_tostring(L,-1); return; } } int main ( int argc, char *argv[] ) { L = luaL_newstate(); //加载lua标准库; luaL_openlibs(L); //加载脚本 ; char* str = "E:\\tiaoshi.lua"; if (luaL_loadfile(L,str)||lua_pcall(L,0,0,0)) { const char* error = lua_tostring(L,-1); cout<< string(error)<<endl; return 0; } tiaoshi(); lua_close(L); //系统命令,暂停; system("pause"); return 0; }
C++测试代码
标签:
原文地址:http://www.cnblogs.com/Black-Crow/p/5167221.html