Solved

Changing directories with FtpSetCurrentDirectory

Posted on 2008-10-22
10
1,688 Views
Last Modified: 2013-12-26
I'm having a problem using this Microsoft WinInet function in my PB Code. Here is what's happening:

I successfully FTP into a server with that has 2 subfolders under the root, lets call the 2 subfolders Images and Items so it looks like this:

//(Root where I'm at after first FTP'ing in)
Images Folder
Items Folder

I can use FtpSetCurrentDirectoryA to go to the Items folder then I FTP some data, that works. The problem is how do I change to the Images folder from the Items folder??. I cant go directly to the folder and I cant seem to figure out how to go up one level or go to the root. I've tried calling my function like this while in the Items folder:

li_rc = FtpSetCurrentDirectoryA( iul_service, 'Images' ) == Return code error
li_rc = FtpSetCurrentDirectoryA( iul_service, ' ..' ) == Tried Space + period + period  to go up one directory still get error.

Of course If I completly disconnect then reconnect to the server I'm placed in the root and I can then go there but I shouldn't have to disconnect from the FTP and come back in just to go to a different directory.

I thought if maybe I use FtpGetCurrentDirectoryA when I first come in maybe I can read the name of the root directory so I can know the name to pass to FtpSetCurrentDirectoryA later on but I can't get this function to work. Here is my code that calls this function but I keep getting an error saying %s Invalid Parameter or something when I call it, here is my code for that.

Integer      li_rc
String            ls_dir, ls_dir2
Ulong            lul_mode
Boolean      lb_rtn
CONSTANT uint MAX_PATH                                                = 254

lul_mode = MAX_PATH


lb_rtn = FtpGetCurrentDirectoryA( iul_service,ls_dir,lul_mode)
iul_errorcode = GetLastError()
IF NOT lb_rtn THEN
      RETURN "-1"
ELSE
      RETURN ls_dir
END IF

Prototypes for both functions:
FUNCTION ulong FtpSetCurrentDirectoryA( ulong hService, string szPath ) LIBRARY "wininet.dll" alias for "FtpSetCurrentDirectoryA;Ansi"

FUNCTION boolean FtpGetCurrentDirectoryA( ulong hService, string szCurrentDirectory, ulong dwCurrentDirectory) LIBRARY "wininet.dll" alias for "FtpGetCurrentDirectoryA;Ansi"









0
Comment
Question by:byteboy11
  • 5
  • 5
10 Comments
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 22779112
below is the syntax for declaration

Function boolean FtpSetCurrentDirectory (ulong hConnect, ref string lpszDirectory) Library "WININET.DLL" Alias for "FtpSetCurrentDirectoryA;Ansi"
Function boolean FtpGetCurrentDirectory (ulong hConnect, ref string lpszCurrentDirectory, ref ulong lpdwCurrentDirectory) Library "WININET.DLL" Alias for "FtpGetCurrentDirectoryA;Ansi"

if you notice some variables are passed as reference and when you pass string as reference you need to assign memory to those variable before passing.

You can do this by

ls_dir = space(1000)    // this is as per requirement.

Hope this will solve problem.

Regards,
Sandeep
0
 

Author Comment

by:byteboy11
ID: 22779844
I added ls_dir = space(1000)
I'm still getting Error: Error calling external function %s at line 30 in function of_getcurrentdirectory of object n_cst_wininet.

Now it looks like this:
-------------------------------------------------------------------------------------
Integer      li_rc
String            ls_dir
Ulong            lul_mode
Boolean      lb_rtn
CONSTANT uint MAX_PATH                                                = 254

lul_mode = MAX_PATH

ls_dir = space(1000)
lb_rtn = FtpGetCurrentDirectoryA( iul_service,ls_dir,lul_mode)
iul_errorcode = GetLastError()
IF NOT lb_rtn THEN
      RETURN "-1"
ELSE
      RETURN ls_dir
END IF
--------------------------------------------------------------------------------------------

What should MAX_PATH be defined as? The MS Doc here doesnt say how to declare it:
http://msdn.microsoft.com/en-us/library/aa384153(VS.85).aspx

0
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 22779997
What is the line 30 in of_getcurrentdirectory ?

try this

CONSTANT uint MAX_PATH      = 260

lul_mode = MAX_PATH

ls_dir = space(MAX_PATH)

Try with some more large values for MAX_PATH if your directory path is suppose to be long. i.e 500, 1000 etc.

Regards,
Sandeep
0
 

Author Comment

by:byteboy11
ID: 22780867
There were comments at the top so I removed them, now the error says line 11. Of course when I debug it does error out on the line that calls the external function so I know its failing there.

Basically I'm still getting the same error. It's like the function isn't defined or something, the parameters seem correct. I checked my version of wininet.dll and it's last modified on 12/07 so it's a fairly recent version of wininet.dll.


----------------------------------------------------------------
Integer      li_rc
String            ls_dir
uint            lul_mode
Boolean      lb_rtn
CONSTANT uint MAX_PATH      = 260

lul_mode = MAX_PATH

ls_dir = space(MAX_PATH)

lb_rtn = FtpGetCurrentDirectoryA( iul_service,ls_dir,lul_mode)
iul_errorcode = GetLastError()
IF NOT lb_rtn THEN
      RETURN "-1"
ELSE
      RETURN ls_dir
END IF
-------------------------------------------------------------------------------------
0
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 22780935
why don't you try once with the declaration i gave?
and use FtpGetCurrentDirectory instead of FtpGetCurrentDirectoryA.

As FtpSetCurrentDirectoryA is working fine, this also should work but still try once.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:byteboy11
ID: 22781089
Here is the code now, when I try to compile I get this error :
*Error    C0116: Reference argument type does not match function definition: ftpgetcurrentdirectory
------------------------------------------------------
Integer                        li_rc
String                              ls_dir
uint                              lul_mode
Boolean                        lb_rtn
ulong                              ul_temp
CONSTANT uint       MAX_PATH      = 500

lul_mode = MAX_PATH

ls_dir = space(MAX_PATH)

lb_rtn = FtpGetCurrentDirectory( iul_service,ls_dir,lul_mode)
iul_errorcode = GetLastError()
IF NOT lb_rtn THEN
      RETURN "-1"
ELSE
      RETURN ls_dir
END IF
------------------------------------------------------------------------------------
local external function:
Function boolean FtpGetCurrentDirectory (ulong hConnect, ref string lpszCurrentDirectory, ref ulong lpdwCurrentDirectory) Library "WININET.DLL" Alias for "FtpGetCurrentDirectoryA;Ansi"
-------------------------------------------------------------------------------------



0
 
LVL 14

Accepted Solution

by:
sandeep_patel earned 500 total points
ID: 22781205
lul_mode should be declared as ulong

ulong lul_mode
0
 
LVL 14

Expert Comment

by:sandeep_patel
ID: 22781224
and yes you can use FtpGetCurrentDirectoryA also as this name is alias for "FtpGetCurrentDirectoryA" as per defination

so sorry about that just you need to add "ref" keyword in declaration.
0
 

Author Closing Comment

by:byteboy11
ID: 31508976
It worked! Declaring it without the 'A' seemed to be the problem. Thanks a lot!
0
 

Author Comment

by:byteboy11
ID: 22781560
Got it, so 'ref' was all that was really needed. I gotta make sure the PB definition  matches the MS definition. Thanks again for pointing that out.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now