What's wrong with this function?

I've written the following function...

////////////////////////////////////////

function MapNetworkDrive(RemoteName, LocalName: String; ReMapAtLogon: Boolean): Integer;
var
  NewMap : NetResource;
begin
  NewMap.lpRemoteName:=PChar(RemoteName);
  NewMap.lpLocalName:=PChar(LocalName);
  NewMap.lpProvider:=nil;
  If ReMapAtLogon then
    Result:=WNetAddConnection2(NewMap, nil, nil, CONNECT_UPDATE_PROFILE)
  else
    Result:=WNetAddConnection2(NewMap, nil, nil, CONNECT_TEMPORARY);
end;

////////////////////////////////////////

If I use the above code within my application (not a seperate function), it works great, but if I reference it as a function, it doesn't work.  Why not?

John.
LVL 6
JaymolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
I think the reason is because you forgot to add:

  NewMap.dwType := RESOURCETYPE_DISK;

and this structure member contains random data so it sometimes works and sometimes not.

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaymolAuthor Commented:
Edited text of question.
0
JaymolAuthor Commented:
I'll try that Mike, but like I said - it works fine outside a function!

John.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

RBertoraCommented:
nil, nil -> have you tried putting a networkname and password?
Rob;-)
0
JaymolAuthor Commented:
Rob: Thanks for the comment, but Mike's got it!  I needed to send it this...

MapNetworkDrive({path}, Chr(TmpI)+':', True)

but it wouldn't work.  With Mike's answer, it does!!!!

(It's just a program to map to the first available drive.)

Thanks Mike!
0
JaymolAuthor Commented:
SUPER!! WONDERFUL!!!

Ta Mike.

John. (.)(.)
0
LischkeCommented:
Glad to help :-)

Ciao, Mike
0
RBertoraCommented:
(.)(.)
yes great!
Rob ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.