IonicWind Software

IWBasic => General Questions => Topic started by: Andy on September 08, 2017, 07:37:45 AM

Title: Unresolved external problem again
Post by: Andy on September 08, 2017, 07:37:45 AM
I came across this function yesterday - RegCopyTreeA - it is a built in function of advapi32.dll, unfortunately it is one of two functions not included in the windows.inc file.

It allows you to copy a registry key to a new key - which is very handy for me - but I keep getting an unresolved external message at compile time.

Linking...
IWBasic Linker v1.11 Copyright © 2011 Ionic Wind Software
Unresolved external __imp_RegCopyTreeA
Error: c:\users\andy\appdata\local\ionicwind\iwbasic3\examples\ren1.o - Unresolved extern __imp_RegCopyTreeA
Error(s) in linking c:\users\andy\appdata\local\ionicwind\iwbasic3\examples\ren1.exe

Now I've checked the dll file and RegCopytreeA does indeed exist, so how do I declare it?

declare "advapi32.dll", RegCopyTreeA(hkey as Uint, Dest as String,hkeyD as Uint),int  ???

If I can get this working it will same me so much time...

Thanks,
Andy.
Title: Re: Unresolved external problem again
Post by: Brian on September 08, 2017, 11:29:02 AM
Andy,

I found a VBA example, and it translated like this:

DECLARE IMPORT,RegCopyTreeA(hKeySrc:INT,lpSubKey:STRING,hKeyDest:INT),INT

Brian
Title: Re: Unresolved external problem again
Post by: fasecero on September 08, 2017, 08:17:15 PM
You can do this by calling the function dynamically. Use Brian's statement as template and call LoadLibrary + GetProcAddress. You can rely on the functions where we convert UINT64 to STRING and vice versa.
Title: Re: Unresolved external problem again
Post by: Andy on September 09, 2017, 04:29:48 AM
Thanks guys,

I took a little time to study what was being suggested and for the first time ever I understand how you load up a dll and use one of it's functions.

I can now use it to copy a key.

Just on the subject of dll's, the other function is NtRenameKey.

Here is a link to msdn:
https://msdn.microsoft.com/en-us/library/cc512138(v=vs.85).aspx

How do I declare this in IWB as the new name has to be UNICODE?

And how do I pass it to a sub directory?

Thanks again, It's a big help!!!
 
Title: Re: Unresolved external problem again
Post by: fasecero on September 09, 2017, 05:24:50 PM
Nice. WSTRING will probably work fine. If not, you have to declare it as POINTER instead.
Title: Re: Unresolved external problem again
Post by: Andy on September 09, 2017, 10:38:12 PM
Why or why did MS have to make this a WString!   :-X

I've tried for hours now but the program crashes, I've tried wstrings and pointers, just can't get it to work.

The ntdll.dll loads okay, so does the function NtRenamekey.

The declare is:

DECLARE CDECL NtRenameKey(hKey as uint,NewKeyName as WSTRING),uINT

and the line which is causing the crash is:

RegRenameResult = !<NtRenameKey>proc(ret,L"HKEY_CURRENT_USER\\Software\\Test6")

It's not a complete disaster, I could just just the copy key function and then delete the original key - but I just wonder if the rename key function will be quicker - and why I can't get it to work.


Title: Re: Unresolved external problem again
Post by: fasecero on September 10, 2017, 04:25:30 PM
Try using single backslash and removing CDECL.
Title: Re: Unresolved external problem again
Post by: Andy on September 11, 2017, 12:33:51 AM
Removing CDECL worked for the copy key function!

Thanks.
:)
Title: Re: Unresolved external problem again
Post by: Andy on September 11, 2017, 05:17:05 AM
Couldn't get the rename key function to work, so I'm copying the key first, then deleting the original as an alternative.
Also, the rename key function is only available for Win Vista and later - and I want our XP friends to be able to use all my new library functions.

I found an error in the old RegdeleteKey function.  It seems that if a key has sub-keys then it will fail.

But I've got the RegDeleteTreeA function working as well, this deletes all Sub keys of a specified key.

So I will amend the delete key function in the new library to check for Sub keys first, if it has some, I will use the delete tree function to remove them, and finally delete the key.