I need to compile a POST request routine using IWebBrowser2 in plain C, the problem is that "the post data specified by PostData is passed as a SAFEARRAY Data Type structure. The VARIANT should be of type VT_ARRAY|VT_UI1 and point to a SAFEARRAY Data Type. The SAFEARRAY Data Type should be of element type VT_UI1, dimension one, and have an element count equal to the number of bytes of post data." as it says in https://learn.microsoft.com/en-us/previous-versions/aa752133(v=vs.85)
When i send the request to my local server it just dont receive the $_POST['name'] nor $_POST['sub'] variables, like i didnt send nothing
INT32
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT32 nShowCmd)
{
HRESULT HResult = 0;
CLSID CLSID_IE;
IWebBrowser2 *pWebBrowser;
VARIANT vEmpty;
VARIANT_BOOL vBusy;
VariantInit(&vEmpty);
CoInitializeEx(0, COINIT_MULTITHREADED);
CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
CoCreateInstance(&CLSID_InternetExplorer, 0, CLSCTX_ALL, &IID_IWebBrowser2, &pWebBrowser);
IWebBrowser2_put_Visible(pWebBrowser, VARIANT_FALSE);
/* process the formulary data */
LPSTR pPostData = NULL;
CHAR strFormulary[] = "sub=gt&name=sdfs";
LPSAFEARRAY FormularyArray = SafeArrayCreateVector(VT_UI1, 0, sizeof(strFormulary));
VARIANT PostData = FormularyArray;
SafeArrayAccessData(FormularyArray, (LPVOID*)&pPostData);
memcpy(pPostData, &strFormulary, sizeof(strFormulary));
SafeArrayUnaccessData(FormularyArray);
/* Set the headers data */
VARIANT Headers;
V_VT(&Headers) = VT_BSTR;
V_BSTR(&Headers) = SysAllocString(L"Content-Type: application/x-www-form-urlencodedrn");
/* Set POST request */
BSTR bstrURL = SysAllocString(L"http://192.168.100.44/user.php");
IWebBrowser2_Navigate(pWebBrowser, bstrURL, &vEmpty, &vEmpty, &PostData, &Headers);
// Wait for page to load
do {
LARGE_INTEGER TimeOut;
UINT32 Milliseconds = 2000;
TimeOut.QuadPart = UInt32x32To64( Milliseconds, 1000 );
TimeOut.QuadPart *= -1;
NtDelayExecution(FALSE, &TimeOut);
IWebBrowser2_get_Busy(pWebBrowser, &vBusy);
} while(vBusy);
// Get IDispatch interface
IDispatch* pDispatch;
IWebBrowser2_get_Document(pWebBrowser, &pDispatch);
IHTMLDocument2* pDocument;
IDispatch_QueryInterface(pDispatch, &IID_IHTMLDocument2 , &pDocument);
IHTMLElement* lpBodyElm;
IHTMLDocument2_get_body(pDocument, &lpBodyElm);
IHTMLElement* lpParentElm;
IHTMLElement_get_parentElement(lpBodyElm, &lpParentElm);
// Get Inner HTML content content of the request
BSTR bstrBody;
lpParentElm->lpVtbl->get_innerHTML(lpParentElm, &bstrBody);
wprintf(L"%ls\n\n", bstrBody);
cleanup:
SysFreeString(bstrURL);
IWebBrowser2_Quit(pWebBrowser);
IWebBrowser2_Release(pWebBrowser);
IDispatch_Release(pDispatch);
IHTMLDocument2_Release(pDocument);
IHTMLElement_Release(lpBodyElm);
lpParentElm->lpVtbl->Release(lpParentElm);
CoUninitialize();
RtlExitUserProcess(STATUS_SUCCESS);
}
i solved it this way: