return array from com object - c++

I want to pass a list of alarm names from COM to VBScript used in ASP pages. If the method name is GetAlarms, What would be the signature of the method?. The number of alarms returned by GetAlarms will vary.
Does VBScrip support Safe Array?

The declaration in the *.idl file would look like this:
[id(1)] HRESULT GetAlarms([out,retval] SAFEARRAY(VARIANT)* pAlarms);
The corresponding C++ method would look like this:
STDMETHODIMP CMyClass::GetAlarms(SAFEARRAY** pAlarms)
{
CComSafeArray<VARIANT> alarms(3);
CComVariant value;
value = L"First Alarm";
alarms.SetAt(0, value);
value = L"Second Alarm";
alarms.SetAt(1, value);
value = L"Third Alarm";
alarms.SetAt(2, value);
*pAlarms = alarms.Detach();
return S_OK;
}
And finally, here is a sample VBScript that uses the above method:
Set obj = CreateObject("MyLib.MyClass")
a = obj.GetAlarms
For i = 0 To UBound(a)
MsgBox a(i)
Next
In ASP, of course, you would use something else instead of MsgBox.

Related

Change div innerHTML through IHTMLDocument2 and C++

I'm trying to change the content of a div using IHTMLDocument2 interface this way:
IHTMLElementCollection* collection = NULL;
IDispatch* mydiv;
doc2->get_all(&collection);
long count;
collection->get_length(&count); //just to check I get something
CComVariant varstr = L"mydivname";
CComVariant varint = 0;
collection->item(varstr, varint, &mydiv); //this works I get the div
IHTMLElement* htmldiv;
mydiv->QueryInterface(IID_IHTMLElement, (void**)&htmldiv);
CComBSTR html;
htmldiv->get_innerHTML(&html); //works too, I get the current content
HRESULT hr=htmldiv->put_innerText(L"hello"); //this does not work but returns S_OK
collection->Release();
So the content of my div is just cleared and not replaced with "hello", I don't understand why, can it be a security issue ?
Thanks
According to the MSDN documentation, the string passed to put_innerText is of type BSTR.
So, I would suggest trying some code like this:
CComBSTR text(OLESTR("hello"));
hr = htmldiv->put_innerText(text);

Converting C++ code to VB.net ( using while )

I'm having trouble parsing some C++ sample code to VB.net ( and yes, i know you won't paste code == do it for me), but i need some guidance.
C++ sample code:
int busy=1;
while (busy == 1)
{
hr = pMarker ->GetBusyStatus (cardnum ,&busy);
}
busy=1;
hr = pMarker ->MarkObj (cardnum,i,90.0);
VB.net code ( that i have, not working properly)
Dim busy As Integer
hr = pMarker.GetBusyStatus(cardnum, busy)
While busy = 1
hr = pMarker.GetBusyStatus(cardnum, busy)
hr = pMarker.MarkObj(cardnum, i, 90.0)
End While
Thank you for your help!
EDIT:
Dim busy As Integer=1
While busy = 1
hr = pMarker.GetBusyStatus(cardnum, busy)
End While
hr = pMarker.MarkObj(cardnum, i, 90.0)
I know that i have to use busy as ByRef, in order to use it like a pointer... So, how can i manage that?
For a parameter to be passed by reference in VB.NET, you need to define it as ByRef in the method definition - there isn't anything in the calling code that indicates it is passed by reference (unlike the C++ & or the C# ref). You didn't show the definition of GetBusyStatus, but it should be something like this:
Public Function GetBusyStatus(cardnum As String, ByRef busy As Integer) As Integer
'...
If someCondition Then
busy = 0
End If
End Function

ADODC retrieve indivisuals values into EditBox

Is there a method to retrieve individual values from ADODC into an edit box
I tried the following way :-
m_edit1=m_adodc1.GetRecordset().GetField().GetItem("table1_names");
but got an error "binary '=' no conversion available .....
as I remember
You can retrieve the value of a field with the Value property, you then need to call the SetWindowText() member function pof the edit control, like:
m_edit1.SetWindowText(m_adodc1.GetRecordset().GetField().GetItem("table1_names").Value());
EDIT: the value needs to be converted using the _bstr_t class:
LPTSTR lpValue = (LPTSTR)(_bstr_t) m_adodc1.GetRecordset().GetField().GetItem("table1_names").Value();
m_edit1.SetWindowText(lpValue);
ADODC or nothing ;)
however check the follwing :-
i made it ,,, partially !!
to inform you about my mdb file :-
its name (inventory.mdb)
it has one table (Stocks)
the fields are (StockName,StockID,...)
now i can extract values from only the "StockName" which its string values
by the following code in button click :-
m_ado.SetRecordSource ("SELECT * FROM Stocks");
m_ado.Refresh ();
C_Recordset m_Record = m_ado.GetRecordset ();// this line can be omitted !
COleVariant var1;
var1.vt = VT_I2;
var1.iVal = 1;
COleVariant value = m_ado.GetRecordset().GetFields().GetItem(var1).GetValue ();
m_edit = value.bstrVal;
UpdateData (FALSE);
how to extract the other values such as "StockID" which its integer or any other fields ?

InternetCrackUrl not returning lpszHostName properly

I am developing a plugin for NSIS (Unicode) and I am trying to use InternetCrackUrl() to get the hostname of a URL (ie: http://www.google.com/test.html -> www.google.com) but instead of lpszHostName just returning "www.google.com", it returns "www.google.com/test.html".
Here is my code:
void __declspec(dllexport) Example(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra) {
g_hwndParent=hwndParent;
EXDLL_INIT();
LPWSTR szURI = new WCHAR[string_size];
URL_COMPONENTS urlComp;
// Sets szURI to "http://www.xyz.com/test.html"
popstring(szURI);
wstring strUri = szURI;
ZeroMemory(&urlComp, sizeof(urlComp));
urlComp.dwStructSize = sizeof(urlComp);
// Set required component lengths to non-zero so that they are cracked.
urlComp.dwHostNameLength = static_cast<DWORD>(-1);
urlComp.dwSchemeLength = static_cast<DWORD>(-1);
urlComp.dwUrlPathLength = static_cast<DWORD>(-1);
urlComp.dwExtraInfoLength = static_cast<DWORD>(-1);
if (!InternetCrackUrlW(strUri.c_str(), strUri.length(), 0, &urlComp)) {
return _T("InternetCrackUrl failed");
}
// urlComp.lpszHostName = www.xyz.com/test.html
}
Any ideas?
If you don't provide your own buffer InternetCrackUrl will return pointers to characters in the original string you pass as input. It doesn't copy the string.
So, lpszHostName will point to the first character, and dwHostNameLength will give you the number of chars that make the host name.
That is the expected behavior.
Because when you say www.google.com it translates to http://www.google.com/test.html.
The URL is really www.google.com/test.html which is what is returned.
To get what you need, you will need to do some string manipulation.
You could use the strrchr function or the find_first_of method of the std::string class.

Word automation - SaveAs

I try to write a simple MFC - Word Automation to save for every 1 minute.
I follow this article : http://www.codeproject.com/KB/office/MSOfficeAuto.aspx
And this is what Im trying to implement , I'm new to COM so I think there's problem here:
my VBA is generated by Word 2010:
ActiveDocument.SaveAs2 FileName:="1.docx", FileFormat:=wdFormatXMLDocument _
, LockComments:=False, Password:="", AddToRecentFiles:=True, _
WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False, CompatibilityMode:=14
And my code to implement VBA code above :
{
COleVariant varName(L"b.docx");
COleVariant varFormat(L"wdFormatXMLDocument");
COleVariant varLockCmt((BYTE)0);
COleVariant varPass(L"");
COleVariant varReadOnly((BYTE)0);
COleVariant varEmbedFont((BYTE)0);
COleVariant varSaveNativePicFormat((BYTE)0);
COleVariant varForms((BYTE)0);
COleVariant varAOCE((BYTE)0);
VARIANT x;
x.vt = VT_I4;
x.lVal = 14;
COleVariant varCompability(&x);;
VARIANT result;
VariantInit(&result);
_hr=OLEMethod( DISPATCH_METHOD, &result, pDocApp, L"SaveAs2",10,
varName.Detach(),varFormat.Detach(),varLockCmt.Detach(),varPass.Detach(),varReadOnly.Detach(),
varEmbedFont.Detach(),varSaveNativePicFormat.Detach(),varForms.Detach(),varAOCE.Detach(),varCompability.Detach()
);
}
I get no error from this one, but it doesn't work.
The VBA syntax uses named parameters where the order and count of the parameters do not matter. However, when calling from C++ you need to pass the required number of parameters in the right order.
SaveAs2 is defined as:
void SaveAs2(
ref Object FileName,
ref Object FileFormat,
ref Object LockComments,
ref Object Password,
ref Object AddToRecentFiles,
ref Object WritePassword,
ref Object ReadOnlyRecommended,
ref Object EmbedTrueTypeFonts,
ref Object SaveNativePictureFormat,
ref Object SaveFormsData,
ref Object SaveAsAOCELetter,
ref Object Encoding,
ref Object InsertLineBreaks,
ref Object AllowSubstitutions,
ref Object LineEnding,
ref Object AddBiDiMarks,
ref Object CompatibilityMode
)
So, it has 17 parameters which means you should specify them all if you are going to pass CompatibilityMode, which was specified as 10th parameter in your example (that corressponds to SaveFormsData).
If you do not need all the parameters, or just for testing, you can try a simpler code:
_hr = OLEMethod(DISPATCH_METHOD, &result, pDocApp, L"SaveAs2", 2, varName.Detach(), varFormat.Detach());
If you need the rest of the parameters, you need to pass all the parameters up to the parameter you need set. In that case, you can pass
COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
for the parameters you do not like to set.
Edit - Test Code
This works for me:
CoInitialize(NULL);
CLSID clsid;
IDispatch *pWApp;
HRESULT hr = CLSIDFromProgID(L"Word.Application", &clsid);
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pWApp);
hr = OLEMethod(DISPATCH_PROPERTYPUT, NULL, pWApp, L"Visible", 1, COleVariant((long)1));
VARIANT result;
VariantInit(&result);
hr = OLEMethod(DISPATCH_PROPERTYGET, &result, pWApp, L"Documents", 0);
IDispatch *pDocs = result.pdispVal;
VARIANT result2;
VariantInit(&result2);
hr = OLEMethod(DISPATCH_METHOD, &result2, pDocs, L"Open", 1, COleVariant(L"D:\\Archive\\t1.docx"));
IDispatch *pDoc = result2.pdispVal;
VARIANT result3;
VariantInit(&result3);
hr = OLEMethod(DISPATCH_METHOD, &result3, pDoc, L"SaveAs2", 1, COleVariant(L"D:\\Archive\\t2.docx"));
CoUninitialize();
Change your variable varFormat from wdFormatXMLDocument to an integer of 12 (probably like you've done the varCompability variable). Also, what's the 10 for after "SaveAs2"?
Guess I'll start over since you opened the bounty.
Change your variable varFormat from wdFormatXMLDocument to an integer of 12 (probably like you've done the varCompability variable). wdFormatXMLDocument is an enum of WdSaveFormat and was introduced in Word 2003. There is no need to send in an L"name" - just send in the integer of 12.
If these are previously saved documents (i.e. not new ones), perform a conversion first to get it to the right format (like ActiveDocument.Convert)