本文目錄一覽:
- 1、linux下c/c++怎麼調用js api
- 2、C語言中,「js」用什麼輸出?
- 3、如何使用c 實現javascript的escape和unescape函數
- 4、在C++中怎麼調用一個js中的方法
linux下c/c++怎麼調用js api
system(執行shell 命令)
相關函數 fork,execve,waitpid,popen
表頭文件 #includestdlib.h
定義函數 int system(const char * string);
函數說明 system()會調用fork()產生子進程,由子進程來調用/bin/sh-c string來執行參數string字符串所代表的命令,此命令執行完後隨即返回原調用的進程。在調用system()期間SIGCHLD 信號會被暫時擱置,SIGINT和SIGQUIT 信號則會被忽略。
返回值 如果system()在調用/bin/sh時失敗則返回127,其他失敗原因返回-1。若參數string為空指針(NULL),則返回非零值。如果system()調用成功則最後會返回執行shell命令後的返回值,但是此返回值也有可能為system()調用/bin/sh失敗所返回的127,因此最好能再檢查errno 來確認執行成功。
附加說明 在編寫具有SUID/SGID權限的程序時請勿使用system(),system()會繼承環境變量,通過環境變量可能會造成系統安全的問題。
範例 #includestdlib.h
main()
{
system(「ls -al /etc/passwd /etc/shadow」);
}
執行 -rw-r–r– 1 root root 705 Sep 3 13 :52 /etc/passwd
-r——— 1 root root 572 Sep 2 15 :34 /etc/shadow
C語言中,「js」用什麼輸出?
把標籤 「htmlscript/div」 中的關鍵字符轉義。
此處轉義的關鍵字主要是 和 。
關鍵字[]轉義為 gt; [移除中間空格]
關鍵字[]轉義為 lt;[移除中間空格]
例如html標籤轉義後為 lt;html gt;
然後調用 document.write(” lt;html gt;”) 顯示在頁面上。
如何使用c 實現javascript的escape和unescape函數
escape() 函數可對字符串進行編碼,這樣就可以在所有的計算機上讀取該字符串。語法escape(string)參數描述string必需。要被轉義或編碼的字符串。返回值已編碼的 string 的副本。其中某些字符被替換成了十六進制的轉義序列。說明該方法不會對 ASCII 字母和數字進行編碼,也不會對下面這些 ASCII 標點符號進行編碼: – _ . ! ~ * ‘ ( ) 。其他所有的字符都會被轉義序列替換。
提示和注釋提示:可以使用 unescape() 對 escape() 編碼的字符串進行解碼。用法與escape()一樣。
舉例:
script type=”text/javascript”
document.write(escape(“Visit W3School!”) + “br /”)
document.write(escape(“?!=()#%”))
/script
輸出:
Visit%20W3School%21
%3F%21%3D%28%29%23%25%26
注釋:ECMAScript v3 反對使用該方法,應用使用 decodeURI() 和 decodeURIComponent() 替代它。
在C++中怎麼調用一個js中的方法
例如一個test.js內容如下:
function main( input )
{
return input;
}
在C++中調用方法如下:
// vcJscript.cpp : 定義控制台應用程序的入口點。
//
#include “stdafx.h”
#import “C:/windows/system32/msscript.ocx” // msscript.ocx
using namespace MSScriptControl;
#include fstream
#include string
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = CoInitialize(NULL);
IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
pScriptControl-Language = “JavaScript”;
//pScriptControl-AllowUI = TRUE;
fstream file;
file.open( “test.js” );
string strFileContent, strTemp;
char szTemp[1024]=””;
do
{
file.read(szTemp, 1024);
strFileContent.append( szTemp );
memset( szTemp, 0, 1024 );
}
while ( !file.fail() );
file.close();
pScriptControl-AddCode(strFileContent.c_str());
VARIANT A = pScriptControl-Eval(“main(4);”);
int iRet = A.intVal;
return 0;
}
腳本控件有四種方法。其中之一是 Run(),運行子例程或函數。在調用此方法之前,指定的腳本語言、 設置 AllowUI,並將下面的腳本添加到腳本控件:
//———————- Begin —————————
#include stdio.h
#import “C:/winnt/system32/msscript.ocx” // msscript.ocx
using namespace MSScriptControl;
int main(void)
{
HRESULT hr = CoInitialize(NULL);
IScriptControlPtr pScriptControl(__uuidof(ScriptControl));
// Create a VARIANT array of VARIANTs which hold BSTRs
LPSAFEARRAY psa;
SAFEARRAYBOUND rgsabound[] = { 3, 0 }; // 3 elements, 0-based
int i;
psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
if (!psa)
{
return E_OUTOFMEMORY;
}
VARIANT vFlavors[3];
for (i = 0; i 3; i++)
{
VariantInit(vFlavors[i]);
V_VT(vFlavors[i]) = VT_BSTR;
}
V_BSTR(vFlavors[0]) = SysAllocString(OLESTR(“Vanilla”));
V_BSTR(vFlavors[1]) = SysAllocString(OLESTR(“Chocolate”));
V_BSTR(vFlavors[2]) = SysAllocString(OLESTR(“Espresso Chip”));
long lZero = 0;
long lOne = 1;
long lTwo = 2;
// Put Elements to the SafeArray:
hr = SafeArrayPutElement(psa, lZero,vFlavors[0]);
hr = SafeArrayPutElement(psa, lOne,vFlavors[1]);
hr = SafeArrayPutElement(psa, lTwo,vFlavors[2]);
// Free Elements from the SafeArray:
for(i=0;i3;i++)
{
SysFreeString(vFlavors[i].bstrVal);
}
// Set up Script control properties
pScriptControl-Language = “JScript”;
pScriptControl-AllowUI = TRUE;
pScriptControl-AddCode(
“function MyStringFunction(Argu1,Argu2,Argu3)/
{ return /”hi there/” ;}” );
// Call MyStringFunction with the two args:
_variant_t outpar = pScriptControl-Run(“MyStringFunction”, psa);
// Convert VARIANT to C string:
_bstr_t bstrReturn = (_bstr_t)outpar;
char *pResult = (char *)bstrReturn;
// Print the result out:
printf(“func=%s/n”,pResult);
// Clean up:
SafeArrayDestroy(psa);
CoUninitialize();
return(0);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/297986.html