 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Oren Halvani Guest
|
Posted: Tue Sep 19, 2006 4:58 am Post subject: Passing files to programm as parameter via a SHELL Context M |
|
|
dear builders,
i want to give my program the functionality to process files through the
the shell (Windows Explorer). Therefore i'm using the below code, which
looks for me a bit strange...(to mannnny if statements) well it works fine,
but i just would like to know if someone can tell me a better way...
i know that i can replace values like:
chkExplorer_Integration->Checked
to:
bool Exp_Integrate = chkExplorer_Integration->Checked;
if(Exp_Integrate)
{
//......
But besides this, have someone other suggestions ?
Oh, and one more question, can someone tell me how to
insert a registry-key structure like this:
HKEY_CLASSES_ROOT\Directory\shell\
|
----- ShowRecord
|-----DefaultIcon
|-----command
Oren
/***********************************************************/
void __fastcall TfrmOptions::chkExplorer_IntegrationClick(TObject *Sender)
{
TRegistry *reg = new TRegistry; reg->RootKey = HKEY_CLASSES_ROOT;
try
{
if(reg->OpenKey("\\Directory\\shell\\", false))
{
if(chkExplorer_Integration->Checked)
{
if(reg->CreateKey("ShowRecord"))
{
if(reg->OpenKey("ShowRecord", false))
{
reg->WriteString("", "Mit &ShowRecord filtern..."); // Write to "Standard"
if(reg->CreateKey("command")) {if(reg->OpenKey("command", false)) reg->WriteString("", Application->ExeName + "
%1");}
if(reg->CreateKey("DefaultIcon"))
{
if(reg->OpenKey("DefaultIcon", false)) // Write to "Standard"
{
reg->WriteString("", Application->ExeName + ",-1");
}
}
}
}
}
else
{
if(reg->KeyExists("ShowRecord")) if(reg->DeleteKey("ShowRecord"));
}
}
reg->CloseKey();
}
__finally
{
delete reg; reg = NULL;
}
}
// inside the constructor...
__fastcall TfrmMainUnit::TfrmMainUnit(TComponent* Owner) : TForm(Owner)
{
for(int i = 1; i <= ParamCount(); i++)
{
AddFiles(ParamStr(i)); // IS THIS SAFE ?????
}
}
// the function...
void __fastcall TfrmMainUnit::AddFiles(String const& Path)
{
TSearchRec searchRecord;
if(FindFirst(Path + "\\*.*", faAnyFile, searchRecord) == 0)
{
do
{
String dir_File = Path + "\\" + searchRecord.Name;
if(searchRecord.Attr & faDirectory) {if(searchRecord.Name[1] != '.') AddFiles(dir_File);}
else
{
IsAscii(dir_File);
FileBox->Items->Add(dir_File);
FileShow(dir_File);
}
}
while(FindNext(searchRecord) == 0); FindClose(searchRecord);
}
} |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Sep 19, 2006 5:35 am Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450f32b5 (AT) newsgroups (DOT) borland.com...
| Quote: | i want to give my program the functionality to process files through
the the shell (Windows Explorer).
|
The code you have shown is not set up properly for what you are attempting.
| Quote: | can someone tell me how to insert a registry-key structure like this:
|
You should take out the calls to CreateKey() altogether since OpenKey() can
create non-existent keys. Also, change your "command" value to wrap %1 in
quotations so that you can handle long folder names. And lastly, you are
not managing the subkeys properly anyway, as you are creating the following
incorrect tree:
HKEY_CLASSES_ROOT\Directory\shell
|
|-- ShowRecord
|-- command
|-- DefaultIcon
Try this code instead:
void __fastcall TfrmOptions::chkExplorer_IntegrationClick(TObject
*Sender)
{
TRegistry *reg = new TRegistry;
try
{
reg->RootKey = HKEY_CLASSES_ROOT;
if( chkExplorer_Integration->Checked )
{
reg->Access = KEY_WRITE;
if( reg->OpenKey("\\Directory\\shell\\ShowRecord", true) )
{
reg->WriteString("", "Mit &ShowRecord filtern...");
if( reg->OpenKey("command", true) )
reg->WriteString("", Application->ExeName + "
\"%1\"");
reg->CloseKey();
}
if( reg->OpenKey("\\Directory\\DefaultIcon", true) )
{
reg->WriteString("", Application->ExeName + ",-1");
reg->CloseKey();
}
}
else
{
reg->Access = (KEY_ENUMERATE_SUB_KEYS | DELETE);
reg->DeleteKey("\\Directory\\ShowRecord");
}
}
__finally
{
delete reg;
}
}
Alternatively, use my free TRegExtension component instead
(http://reg.lebeausoftware.org), which can handle those details for you.
| Quote: | // inside the constructor...
|
Try this code instead:
__fastcall TfrmMainUnit::TfrmMainUnit(TComponent* Owner)
: TForm(Owner)
{
int iCount = ParamCount();
for(int i = 1; i <= iCount; ++i)
AddFiles(ParamStr(i));
}
void __fastcall TfrmMainUnit::AddFiles(const AnsiString &Path)
{
DWORD dwAttr = GetFileAttributes(Path.c_str());
if( dwAttr != 0xFFFFFFFF )
{
if( dwAttr & FILE_ATTRIBUTE_DIRECTORY )
InternalAddFiles(Path);
else
AddFile(Path);
}
}
void __fastcall TfrmMainUnit::InternalAddFiles(const AnsiString &Folder)
{
TSearchRec searchRecord;
AnsiString asFolder = IncludeTrailingBackslash(Folder);
if( FindFirst(asFolder + "*.*", faAnyFile, searchRecord) == 0 )
{
try
{
do
{
AnsiString file = asFolder + searchRecord.Name;
if( searchRecord.Attr & faDirectory )
{
if( (searchRecord.Name != ".") && (searchRecord.Name
!= "..") )
InternalAddFiles(file);
}
else
AddFile(file);
}
while( FindNext(searchRecord) == 0 );
}
__finally {
FindClose(searchRecord);
}
}
}
void __fastcall TfrmMainUnit::AddFile(const AnsiString &FileName)
{
IsAscii(FileName);
FileBox->Items->Add(FileName);
FileShow(FileName);
}
Gambit |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Tue Sep 19, 2006 5:55 am Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb im Newsbeitrag
news:450f3bc8$1 (AT) newsgroups (DOT) borland.com...
Remy, first of all thanks for your great reply....
| Quote: | Try this code instead:
reg->Access = KEY_WRITE;
|
i'm using for this project a BCB 3 Version, where the property 'Access'
is not implemented...is there another replacement for that property ?
Oren |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Sep 19, 2006 8:10 am Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450f4010 (AT) newsgroups (DOT) borland.com...
| Quote: | i'm using for this project a BCB 3 Version, where the property
'Access' is not implemented...is there another replacement for
that property ?
|
The TRegistry::Access property was introduced in BCB 5. The only way to
specify access rights for the Registry in older versions is to not use
TRegistry at all, but to call the Win32 Registry API functions directly
instead, ie:
void __fastcall DeleteSubKey(HKEY hKey, LPCTSTR KeyName)
{
HKEY hSubKey = NULL;
LONG lRet = RegOpenKeyEx(hKey, KeyName, 0,
KEY_ENUMERATE_SUB_KEYS | DELETE, &hSubKey);
if( lRet == ERROR_SUCCESS )
{
DWORD NumSubKeys = 0;
DWORD MaxSubKeyLen = 0;
lRet = RegQueryInfoKey(hSubKey, NULL, NULL, NULL,
&NumSubKeys, &MaxSubKeyLen, NULL, NULL,
NULL, NULL, NULL, NULL);
if( NumSubKeys > 0 )
{
DWORD dwLen = (MaxSubKeyLen+1);
DWORD dwSize = (sizeof(TCHAR) * dwLen);
LPTSTR lpName = (LPTSTR) LocalAlloc(LMEM_FIXED, dwSize);
if( lpName )
{
DWORD dwIndex = 0;
do
{
ZeroMemory(lpName, dwSize);
dwLen = (MaxSubKeyLen+1);
lRet = RegEnumKeyEx(hSubKey, dwIndex++, lpName,
&dwLen, NULL, NULL, 0, NULL);
if( lRet != ERROR_SUCCESS )
break;
DeleteSubKey(hSubKey, lpName);
}
while( true );
}
}
RegCloseKey(hSubKey);
}
RegDeleteKey(hKey, KeyName);
}
void __fastcall TfrmOptions::chkExplorer_IntegrationClick(TObject
*Sender)
{
HKEY hKey = NULL;
LONG lRet;
if( chkExplorer_Integration->Checked )
{
DWORD dwDisposition = 0;
lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT,
TEXT("Directory\\shell\\ShowRecord"), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
&hKey, &dwDisposition);
if( lRet == ERROR_SUCCESS )
{
RegSetValueEx(hKey, TEXT(""), 0, REG_SZ,
(LPBYTE) "Mit &ShowRecord filtern...", 27);
HKEY hSubKey = NULL;
dwDisposition = 0;
lRet = RegCreateKeyEx(hKey, TEXT("command"), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
&hSubKey, &dwDisposition);
if( lRet == ERROR_SUCCESS )
{
AnsiString s = Application->ExeName + " \"%1\"";
RegSetValueEx(hSubKey, TEXT(""), 0, REG_SZ, (LPBYTE)
s.c_str(), s.Length()+1);
RegCloseKey(hSubKey);
}
RegCloseKey(hKey);
}
hKey = NULL;
dwDisposition = 0;
lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT,
TEXT("Directory\\DefaultIcon"), 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
&hKey, &dwDisposition);
if( lRet == ERROR_SUCCESS )
{
AnsiString s = Application->ExeName + ",-1";
RegSetValueEx(hKey, TEXT(""), 0, REG_SZ, (LPBYTE) s.c_str(),
s.Length()+1);
RegCloseKey(hKey);
}
}
else
DeleteSubKey(HKEY_CLASSES_ROOT, TEXT("Directory\\ShowRecord"));
}
Gambit |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Tue Sep 19, 2006 4:40 pm Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb im Newsbeitrag
news:450f71b4$1 (AT) newsgroups (DOT) borland.com...
| Quote: | The TRegistry::Access property was introduced in BCB 5. The only way to
specify access rights for the Registry in older versions is to not use
TRegistry at all, but to call the Win32 Registry API functions directly
instead, ie:
|
[code...]
Hi Remy,
Thanks for the API workaround..anything beside the following line works great:
| Quote: | else DeleteSubKey(HKEY_CLASSES_ROOT, TEXT("Directory\\ShowRecord"));
|
i've tried also this:
else
{
LONG RetVal = DeleteSubKey(HKEY_CLASSES_ROOT, TEXT("Directory\\ShowRecord"));
if(RetVal == ERROR_SUCCESS) ShowMessage("Key r emoved");
}
but the compiler drops me the error: "Value of type void is not allowed" WHY?
MSDN says: (http://msdn.microsoft.com/library/en-us/dv_wceatl4/html/ealrfCRegKeycolcolDeleteSubKey.asp?frame=true)
that DeleteSubKey is from type LONG ?
LONG DeleteSubKey(LPCTSTR lpszSubKey );
so why the compiler talksabout void ??
by the way, the compiler drops me also a warning that: "IRet is assigned a value that is never used"
Should i ignore this warning ? What causing this ? IRet is used by this, so why the warning occurs ?
lRet = RegCreateKeyEx(HKEY_CLASSES_ROOT, TEXT("Directory\\shell\\ShowRecord"),
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition);
Oren |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Tue Sep 19, 2006 9:53 pm Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:450fd727$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Thanks for the API workaround..anything beside the following
line works great:
|
Change it to this instead:
DeleteSubKey(HKEY_CLASSES_ROOT, TEXT("Directory\\shell\\ShowRecord"));
| Quote: | i've tried also this:
snip
but the compiler drops me the error: "Value of type void is not allowed"
WHY? |
Because the DeleteSubKey() function I wrote has no return value, so you
can't assign it to anything.
That does not apply to this issue at all. That documentation refers to the
CRegKey ATL class for Windows CE. The code I gave you does not use that at
all.
| Quote: | by the way, the compiler drops me also a warning that: "IRet is assigned
a value that is never used" Should i ignore this warning ?
|
Yes.
| Quote: | What causing this ?
|
Did you read through all of the code yet? There are places where lRet is
assigned but not being used.
| Quote: | IRet is used by this, so why the warning occurs ?
|
There are several other places where lRet is assigned.
Gambit |
|
| Back to top |
|
 |
Oren Halvani Guest
|
Posted: Wed Sep 20, 2006 4:02 am Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb im Newsbeitrag
news:45102115$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
Change it to this instead:
DeleteSubKey(HKEY_CLASSES_ROOT, TEXT("Directory\\shell\\ShowRecord"));
|
thanks...works PERFECT !!
| Quote: | MSDN says:
That does not apply to this issue at all. That documentation refers to the
CRegKey ATL class for Windows CE. The code I gave you does not use that at
all.
|
oh...didn't know that...i was looking first in the Win32 API help for that function, but
i couldn't find it there..so i've searched google for it to know what value it returns..
MSDN was the first match what i've found so i've thought that it realy returns a LONG value....
anyhow...now i know more about this function :-)
thanks again Remy..
Oren |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Sep 20, 2006 4:35 am Post subject: Re: Passing files to programm as parameter via a SHELL Conte |
|
|
"Oren Halvani" <Spam (AT) halvani (DOT) de> wrote in message
news:4510772b (AT) newsgroups (DOT) borland.com...
| Quote: | thanks...works PERFECT !!
|
Don't forget to restore the old DefaultIcon value if/when you remove your
ShowRecord key.
Gambit |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|