Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Error 1607 when running VB EXE as NT service

Posted on 2004-08-08
Medium Priority
Last Modified: 2013-12-25
Hi all ,

following is the process

I have created an NT service thru VB and i have also started the service and stopped the service succesfully .

The problem is i want to do some operation after starting the service.

Actually i have a VB exe which accepts command line paramteres (among which one of them is the network drive to map) and accordingly maps network drive mentioned.

i want to run this EXE after starting the service ... and the command line paramters will be supplied by user in "SERVICES" window in "Administrative Tools" in control panel....

The user will run a VB app to create a service. then the will go to "Services" window and  select the service .. right click and start it physically and also give paramters ...

WHat happens is that when we start the service ... "error 1607 The process terminated unexpectedly"

i am using MS ocx file "NTVBsvc" lib file ..

i used 2 methods to map :

i use wscript.network to map drives
This runs as a service and maps .. but maps as "Disconnnected NetworkDrive (D:)"  ... and it also is not consistent with user name and password.

second method is wnetaddconnection2 . here the service tries to start mapping but stops throwing the above mentioned error

So what i did was , instead of calling an exe (which contains coding to map) after starting a service ... i did the mapping after the line which starts the service ...instead of calling a separate exe to do the same

But i am not able to do it ..  normal operations like "filecopy" and Writing to a text file can be done after connecting a service .. but not mapping

I think i have clearly stated the issue.. Can anyone help me out ??? its real crisis for me..

the sample source code basd on which i had done my coding is in the follwoing link


In this code ... In "Service " folder
Open "Service Sample.bas"

In "Main " module ...

        Loop While WaitForSingleObject(hStopPendingEvent, 1000&) = WAIT_TIMEOUT

i think everthing is right out there ... so i am positive that some body will solve it ...

OS is XP / win 2K


Question by:deandany
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
LVL 22

Expert Comment

ID: 11751173
What about showing the windows' mapping dialog? It would also be easier to use that then specifying map path manually in service properties.. Here is the code:

Private Declare Function WNetConnectionDialog Lib "mpr" (ByVal hwnd As Long, ByVal dwType As Long) As Long

Public Sub ShowMapDialog()
    WNetConnectionDialog ByVal 0&, RESOURCETYPE_DISK
End Sub

There is also another, simpler way to map the network drive:
Shell "net use X: \\path_to_map\share", vbHide


Author Comment

ID: 11753566
Hi  DarkoLord

Thanks for the reply .. but net use does not solve the purpose just bcos it does not accept login info.

And even WnetConnectDialog is not recommendable cos it will confuse the user. it should happen in the background.

Just stick to the VB code i have mentioned ... The main issue is mapping after running the service. Shell does not execute properly .

- Dinesh

LVL 22

Expert Comment

ID: 11753679
Okay I'll stick to your code, however it is possible to use username&pass with net use:

net use X: \\path_to_map\share /USER:administrator password

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11779008
hi darko

I have progressed a bit in my project.

I will narrow down my problem

now i am able to install a service, Start it , Stop it and uninstall it.

the only problem is that i have an exe which will execute when service is started ( the exe assigned in "Path of executable" in service window) .

this exe has coding to Map a drive using wnetaddconnection2 api . when i map without username and password.. i am able to map drives after starting service.

But the problem arises when i try passing valid username and password to Wnetaddconnection2 api

actually i am able to map using same user,pass word manulally

the error i am getting is error 1312 : the logon session has already been terminated

So i think there is some login issue cos of Windows setting etc and not in my mappin coding

i think this is helpful


Author Comment

ID: 11791146

I encountered the following error when creating a service application using C#, Vb.net and installing it the service using Installutil.exe (in command prompt) with Service.Account as "User".
-----------------------------------------------------------------------An exception occurred during the Install phase.
System.ComponentModel.Win32Exception: The account name is invalid or does not ex
ist, or the password is invalid for the account name specified

I have created a User account with "Log on as service" privilage

still i get this error.

Any feedbacks?


LVL 22

Accepted Solution

DarkoLord earned 2000 total points
ID: 11791604
Hmm try specifying "COMPUTERNAME\Username" as username...


Author Comment

ID: 11793462
Great !!!! thanks for the timely help ....

But is there any workaround for this .. cos we cannot ask the user to give machine name ...

Anyways thanks buddy ... u earned these points

LVL 22

Expert Comment

ID: 11793798
You're welcome... anyway try to use this function to get computer name:

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function fOSMachineName() As String

    Dim lngLen As Long, lngx As Long
    Dim strCompName As String
    lngLen = 16
    strCompName = String(lngLen, 0)
    lngx = GetComputerName(strCompName, lngLen)
    If lngx <> 0 Then
        fOSMachineName = Left(strCompName, lngLen)
        fOSMachineName = ""
    End If
End Function


Author Comment

ID: 11805147
however i have one problem still

even though i am able to install a service , start it and stop it ... i am not able to map a network drive after starting the service ... i think Wnetaddconnection2 is not compatibel ..

or there could be one more issue ... i think the system user login is getting overlapped with the user login given to map to a network drive ...

Cos i am able to do everything else but to map a network drive after starting the service

Can anyone try this out and send me a piece of code that installs and starts an NT service and maps to a network drive with username and password .. (for now hard code the login info) ..

I need it to be done either in VB.net or C#.net ..

Use service.account = "User"


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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