Onega

a lot of VC++ posts, a few C# posts, and some miscellaneous stuff

Thursday, November 18, 2004

run program with wsh

Dim wsh
Set wsh = CreateObject("WScript.Shell")


intReturn = Wsh.Run("notepad ", 1, True)
Set wsh = Nothing

Dim ArgObj
Set ArgObj = WScript.Arguments
For each arg in ArgObj
MsgBox arg
next

Tuesday, November 16, 2004

VBscript that clear temporary files of generated by VC

'*************clear temporary files of generated by VC++, will delete readonly files
'*************usage: clear.vbs C:\temp
'*************usage: clear.vbs "C:\old source\7-23-04"
'*************Author: Onega(www.fruitfruit.com http://fruitfruit.blogspot.com)
'*************Warning: Use it at your own risk!


Dim ArgObj
Set ArgObj = WScript.Arguments

'***************create array containing postfix of file names. Those files are to be deleted
Dim dict
Set dict = CreateObject("Scripting.Dictionary")
dict.Add dict.count , ".obj" 'Object File
dict.Add dict.count , ".ncb" 'Visual C++ Intellisence Database
dict.Add dict.count , ".plg"
dict.Add dict.count , ".aps"
dict.Add dict.count , ".ilk"
dict.Add dict.count , ".pch"
dict.Add dict.count , ".pdb"
dict.Add dict.count , ".idb"
dict.Add dict.count , "BuildLog.htm"
dict.Add dict.count , ".tli"
dict.Add dict.count , ".tlh" '
dict.Add dict.count , ".suo" 'Visual Studio Solution User Options
dict.Add dict.count , ".opt"
dict.Add dict.count , ".sbr" 'Source Browser Intermediate File
dict.Add dict.count , ".bsc" 'Source Browser Database
'dict.Add dict.count , ".res" '

On Error Resume Next


If ArgObj.Count>0 Then
object_folder = ArgObj(0)
Set IFileSystem3 = CreateObject("Scripting.FileSystemObject")
set IFolder = IFileSystem3.GetFolder(object_folder)
clear_folder IFolder
MsgBox "finished"
Else
MsgBox "Usage: clear.vbs C:source"
End If


Function clear_folder( folder )
set IFileCollection = folder.Files
For each file in IFileCollection
If is_temp_file( file.Path ) Then
file.Delete true
If Err <> 0 Then
MsgBox "Error # " & CStr(Err.Number) & " " & Err.Description & " file:" & file.Path
Err.Clear ' Clear the error.
End If

End If
Next

For each sub_folder in folder.SubFolders
clear_folder sub_folder
Next
End Function



Function end_with( src_string, key)
end_with = false
If Len(key) <1 Then
end_with = false
Exit Function
End if
If Len(src_string) < Len(key) Then
end_with = false
Else
Dim temp_str
temp_str = right(src_string, Len(key) )
'vbTextCompare = 1
If StrComp( temp_str,key,vbTextCompare) = 0 Then
end_with = true
Else
end_with = false
End if
End if

End function

Function is_temp_file(path)
is_temp_file = false
For each postfix in dict.items
If end_with(path, postfix) Then
is_temp_file = true
Exit for
End if
next
End function

Monday, November 15, 2004

Use IDispatch interface

Author: Onega(www.fruitfruit.com)
There are two wrappers provided in MSDN, invhelp.cpp implement
HRESULT CreateObject(LPOLESTR pszProgID, IDispatch FAR* FAR* ppdisp);
HRESULT
Invoke(LPDISPATCH pdisp,
WORD wFlags,
LPVARIANT pvRet,
EXCEPINFO FAR* pexcepinfo,
UINT FAR* pnArgErr,
LPOLESTR pszName,
LPCTSTR pszFmt,
...);
But I have problem using it.
So I found another one,
HRESULT AutoWrap(int autoType, VARIANT *pvResult, IDispatch *pDisp,
LPOLESTR ptName, int cArgs...);
and extended it to
HRESULT AutoWrapEx(int autoType, VARIANT *pvResult, IDispatch *pDisp,
LPOLESTR ptName,
EXCEPINFO* pexcepinfo,//
UINT* pnum_of_Arg_Error,
int cArgs...);
The problem with its original implementation is that user has to reverse order of arguments passed to IDispatch via AutoWrap. I decide to remove the inconvenience, so my version looks like the following:
//caller is responsible for releasing the incoming VARIANT via VariantClear
HRESULT AutoWrapEx(int autoType, VARIANT *pvResult, IDispatch *pDisp,
LPOLESTR ptName,
EXCEPINFO* pexcepinfo,//
UINT* pnum_of_Arg_Error,
int cArgs...)
{
// Begin variable-argument list...
va_list marker;
va_start(marker, cArgs);

if(!pDisp) {
return E_INVALIDARG;
}
// Variables used...
DISPPARAMS dp = { NULL, NULL, 0, 0 };
DISPID dispidNamed = DISPID_PROPERTYPUT;
DISPID dispID = 0;
HRESULT hr = S_OK;
char buf[200];
// Get DISPID for name passed...
hr = pDisp->GetIDsOfNames(IID_NULL, &ptName, 1, LOCALE_USER_DEFAULT,
&dispID);
if(FAILED(hr)) {
return hr;
}
// Allocate memory for arguments...
VARIANT *pArgs = new VARIANT[cArgs+1];
// Extract arguments...
for(int i=0; i pArgs[cArgs-1-i] = va_arg(marker, VARIANT);//reverse the arg order
}
// Build DISPPARAMS
dp.cArgs = cArgs;
dp.rgvarg = pArgs;
// Handle special-case for property-puts!
if(autoType & DISPATCH_PROPERTYPUT) {
dp.cNamedArgs = 1;
dp.rgdispidNamedArgs = &dispidNamed;
}
// Make the call!
hr = pDisp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, autoType,
&dp, pvResult, pexcepinfo, pnum_of_Arg_Error);
// End variable-argument section...
va_end(marker);
delete [] pArgs;
return hr;
}

Sample usage:
IDispatchPtr pScriptControl(__uuidof(CLSID_ScriptControl));
_variant_t sitewnd((long)this->m_hWnd,VT_I4);
VARIANT ret;
VariantInit(&ret);
hr = AutoWrap(DISPATCH_PROPERTYPUT| DISPATCH_METHOD, NULL,pScriptControl,L"SitehWnd",1,sitewnd);
if(FAILED(hr))
return;
//http://support.microsoft.com/kb/184977/EN-US/
//FIX: ScriptControl Reports Invalid Language for VBScript in MFC
hr = AutoWrap(DISPATCH_PROPERTYPUT| DISPATCH_METHOD, NULL,pScriptControl,L"Language",1,_variant_t(_T("")) );
if(FAILED(hr))
return;
hr = AutoWrap(DISPATCH_PROPERTYPUT| DISPATCH_METHOD, NULL,pScriptControl,L"Language",1,_variant_t(language));
if(FAILED(hr))
return;
hr = AutoWrap(DISPATCH_PROPERTYPUT| DISPATCH_METHOD, NULL,pScriptControl,L"AllowUI",1,_variant_t(VARIANT_TRUE));
if(FAILED(hr))
return;
if(m_pControl && m_pControl->GetControlUnknown())
{
IDispatch* pControl=NULL;
hr = m_pControl->GetControlUnknown()->QueryInterface(IID_IDispatch,(void**)&pControl);
_variant_t name(control_name);
_variant_t obj(pControl);
_variant_t AddMembers( VARIANT_FALSE,VT_BOOL );
hr = AutoWrapEx(DISPATCH_METHOD,
NULL,
pScriptControl,
L"AddObject",
&excepInfo,
&nArgErr,
3,
name,
obj,
AddMembers
);
}
//var d; d = new ActiveXObject("Scripting.Dictionary");
//d.Add( "a" , "Athens" ) ;
//d.Items()
//for jscript
// Create a VARIANT array of VARIANTs which hold BSTRs
//http://support.microsoft.com/kb/165967/EN-US/
//PRB: Script Error Occurs When Referencing Non-variant Array
IDispatchPtr scriptdic(_T("Scripting.Dictionary")); //
if(scriptdic)
{
hr = AutoWrap(DISPATCH_METHOD,NULL,pScriptControl,L"AddObject",3,
_variant_t(_T("jarray")),
_variant_t((IDispatch *)scriptdic.Detach()),
_variant_t(false)
);
}
_variant_t result;
hr = AutoWrapEx(DISPATCH_METHOD, &result,pScriptControl,L"AddCode",&excepInfo,&nArgErr,1,_variant_t(code_start));

Thursday, November 11, 2004

CreateControl and destroy at runtime in MFC project.

Author: Onega (www.fruitfruit.com)
To create activex control, place an invisible static control(IDC_STATIC_TMP) on dialog as a place holder.

CWnd *m_pControl; //initialize it to NULL in ctor
int m_top_offset ;
int m_left_offset ;
int m_bottom_offset ;
int m_control_width;

in CxxxDlg::InitDialog()
{
...
CRect static_rect;
GetDlgItem(IDC_STATIC_TMP)->GetWindowRect(&static_rect);
ScreenToClient(&static_rect);
CRect rc;
GetClientRect(&rc);
m_top_offset = static_rect.top-rc.top;
m_left_offset = static_rect.left - rc.left;
m_bottom_offset = rc.bottom - static_rect.bottom;
m_control_width = static_rect.Width();
OnBnClickedInstall();
}

void CxxxDlg::OnBnClickedDismantle()
{
//destroy the control
if(m_pControl)
{
m_pControl->DestroyWindow();
delete m_pControl;
m_pControl = NULL;
}
}

void CxxxDlg::OnBnClickedInstall()
{
//create the control
OnBnClickedDismantle();
//found CLSID from xxx.odl file
//[
// uuid(D2CC1991-C593-11D0-8B19-0020AF14090F),
// helpstring("DoveCtrl Class")
//]
//coclass DoveCtrl
//{
// [default] interface IDoveCtrl;
//};
static CLSID const control_clsid
= { 0xd2cc1991, 0xc593, 0x11d0, { 0x8b, 0x19, 0x0, 0x20, 0xaf, 0x14, 0x9, 0xf } };
OnBnClickedDismantle();
m_pControl = new CWnd;
CRect rc;
GetClientRect(&rc);
rc.left += m_left_offset;
rc.top += m_top_offset;
rc.bottom -= m_bottom_offset;
rc.right = rc.left + m_control_width;
CPoint lt(rc.left,rc.top);
CSize sz(rc.Width(),rc.Height());
m_pControl->CreateControl(control_clsid,_T("DoveControl"),WS_CHILD|WS_VISIBLE,
<,&sz,this,IDC_STATIC_TMP+1); HWND hWnd = m_pControl->Detach();
m_pControl->SubclassWindow(hWnd);

}

void CxxxDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

// resize the control
if(m_pControl)
{
RECT rc;
this->GetClientRect(&rc);
rc.left += m_left_offset;
rc.top += m_top_offset;
rc.bottom -= m_bottom_offset;
rc.right = rc.left + m_control_width;
m_pControl->MoveWindow(&rc);
::RedrawWindow(m_hWnd,NULL,NULL,RDW_ALLCHILDREN |
RDW_UPDATENOW |
RDW_INVALIDATE);
return;
}

}

Wednesday, November 10, 2004

linker error LNK2005, _DllMain@12 already defined

Author: Onega (www.fruitfruit.com)
The project is an ATL ActiveX DLL project, does not use MFC. It compiles OK. There are only two link warnings.
LINK : warning LNK4098: defaultlib "LIBCMTD" conflicts with use of other libs; use /NODEFAULTLIB:library
MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib "msvcrt.lib" conflicts with use of other libs; use /NODEFAULTLIB:library

After I add a file to the project, which use STL (no MFC). Then I got the link error.

I read http://support.microsoft.com/kb/148652/EN-US/ (Q148652)
create Forcelib.h and include it in stdafx.h, it does not solve the problem.
I put MSVCRTD.lib,mfcs42d.lib in Ignore libraries, and put mfcs42d.lib MSVCRTD.lib in Object/library modules, it does not help.


Oh, My god.
The first thing I add a new .cpp to the project is #include "stdafx.h" in the first line of the cpp.
But after I remove this line, the project can be linked!

Strange enough:
if I don't #include "myfile.h" in imagintr.h
then the compiler accepts
class ImageView
{
...
ImageView::Init(ImageDispRegion *pdisprgn, ImageIntrf *pii);
...
}
After "myfile.h" is included, the compiler complains.
So I change it to:
class ImageView
{
...
int Init(ImageDispRegion *pdisprgn, ImageIntrf *pii);
...
}

after some other staff is added to myfile.cpp,
#include
...
stringstream os;
oss<<__file__<<__line__;>

...
OutputDebugString("aa");
...



After numerous effort, I found that project settings was chagned to Debug Multithreaded instead of Debug Multithreaded DLL.

After I change to /MDd, everything works fine now.

I just want to scold VC++ for its uninformative errors.
Q148652 is badly written. It is the first time I want to add comment to that article, but I can't find a link to comment it.


Tuesday, November 09, 2004

recource for VC and script

The post is written by Onega(www.fruitfruit.com)
//var d; d = new ActiveXObject("Scripting.Dictionary");
//d.Add( "a" , "Athens" ) ;
//d.Items()
Array passed from jscript via d.Items() , type is VT_ARRAY | VT_VARIANT
SAFEARRAY* psa = NULL;
VARIANT *varImages = NULL;
BSTR* bstrImages = NULL;
if(newVal.vt == (VT_ARRAY | VT_VARIANT) )
{
psa = newVal.parray;
SafeArrayAccessData(psa, (void**) &varImages);
}

type of array passed by vbscript is [2416] incoming vt=0000400c, VT_BYREF | VT_VARIANT

if(newVal.vt VT_BYREF|VT_VARIANT)
{
VARIANT *ptmp = newVal.pvarVal;
VARTYPE vtmp = ptmp->vt; // 0x600c VT_BYREF|VT_ARRAY|VT_VARIANT
if(vtmp & VT_BYREF)
psa = *(ptmp->pparray);
else
psa = ptmp->parray;
SafeArrayAccessData(psa, (void**) &varImages);
}
else
{
psa = newVal.parray;
SafeArrayAccessData(psa, (void**) &bstrImages);
}

http://support.microsoft.com/kb/218454/EN-US/
How To Implement Array Arguments in Visual C++ COM Objects for Active Server Pages


http://www.codeguru.com/Cpp/misc/misc/article.php/c3907/
Hosting VBScript in your own Application
Perry Bruins

http://www.codeguru.com/Cpp/misc/misc/article.php/c6093/
Scripter Library
Ivan Martynov

http://www.codeguru.com/Cpp/COM-Tech/atl/scripting/article.php/c23/
Implementing Active Script Site with ATL
Leonid Belkind

http://www.codeguru.com/Cpp/COM-Tech/activex/scripting/article.php/c2563/
ActiveX script hosting
Andrew Garbuzov

http://www.codeguru.com/Cpp/COM-Tech/activex/scripting/article.php/c2583/
Adding Debug facilities to an Active Scripting Host
Stuart Lodge

http://www.codeguru.com/Cpp/COM-Tech/activex/scripting/article.php/c2613/
ActiveX script hosting (2)
Andrew Garbuzov

http://www.codeguru.com/Cpp/COM-Tech/activex/controls/article.php/c5559/
Adding Scripting Support to an Application
Yuri Polyakov

http://www.codeproject.com/com/scripter.asp
Embeddable script editor for MFC applications
By Alex Hazanov

Monday, November 08, 2004

DICOM groups

The following are DICOM groups
http://groups.google.com/groups?hl=en&lr=&group=comp.protocols.dicom
http://groups.google.com/groups?hl=en&lr=&group=alt.image.medical
http://groups.google.com/groups?hl=en&lr=&group=sci.med.radiology

welcome to www.fruitfruit.com

This is my personal homepage. No advertisements.
This is my first post to the blogger at 20041109