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

Error 1607 when running VB EXE as NT service

Posted on 2004-08-08
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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 91
Validating VB6 Function 19 65
VBA error replacing data 6 40
Export Data to Different .csv Files 26 114
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

838 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