0

I'm trying to write a program, that adds a network connection with WNetAddConnection2() and then renames the newly created network drive. The only way I found to do it, is either with PowerShell, or manually editing the registry.

The PowerShell way might be ok for C++ application, but my application has to be in C.

The registry edit was the former way how I renamed the folder, but when my application is run as a system service, modifying HKCU is not trivial. Even if I modify the registry, I have to modify it per user, which doesn't change the label for new users.

This is the batch script I used previously to change the label:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##computername#sharename" /v  "_LabelFromReg" /t REG_SZ /d "label" /f

For some reason calling SetVolumeLabel() doesn't work at all.

Is there any other way to do this? Or at least some recommended way to change the registry for all users?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • The powershell is equally OK or not OK independently of the language (C or C++). – Jabberwocky Mar 10 '16 at 15:00
  • I didn't find a way to call a powershell script from C, only from C++. Could you point me in the rigth direction please? – Spadeas Mar 10 '16 at 15:03
  • How do you call powershell from c++? It should be the same from C. BTW: I'm not sure if you can call powershell from a service. – Jabberwocky Mar 10 '16 at 15:07
  • I found the code here: http://stackoverflow.com/questions/4460390/how-to-rename-or-relabel-a-network-drive-label – Spadeas Mar 10 '16 at 15:16
  • OK, as COM is involved that's not quite easy to get it right in C, but what prevents you from compiling your C code with the C++ compiler. C++ is more or less a superset of C. – Jabberwocky Mar 10 '16 at 15:19
  • I'm not really in charge of saying what language will be used, so I have to work with what I have. – Spadeas Mar 10 '16 at 15:26
  • Ask the person who is in charge. There is no reason why you shouldn't use the C++ compiler. Most C code can be compiled with the C++ compiler. – Jabberwocky Mar 10 '16 at 15:37
  • 2
    Failing a C++ compiler, you can certainly use COM from C, it is just ugly. I can't help but wonder, though, why in the world you are mapping a network drive from a *service*. That just doesn't make sense. These drive mappings only make sense from within an interactive console session, which you are not guaranteed with a service. What is wrong with just using the UNC path? Essentially all Windows APIs support this. – Cody Gray - on strike Mar 10 '16 at 16:11
  • @MichaelWalz C++ supports managed extensions whereas C doesn't. All the examples for hosting Powershell I've seen are from C#. Translating from C# to managed C++ is straightforward. However translating from a managed language to an unmanaged language is quite involved. – Χpẘ Mar 11 '16 at 09:57
  • @Χpẘ: Except, those hosting interfaces are exposed as COM interfaces. Translating COM code from a managed language to an unmanaged language is tedious, but straight forward. Whether you transcribe this to C++ or C doesn't make the job any more challenging, only more tedious in case of C. – IInspectable Mar 11 '16 at 12:13
  • @CodyGray We have a guest VM service, that handles shared folder as a network drive. The way we used to do it was with a batch script, which is not really user friendly. I am trying to do this more user friendly, so that when enabling the shared folder, it automatically maps the drive. – Spadeas Mar 11 '16 at 20:22
  • @IInspectable I was responding to MichaelWalz comments about hosting PS from C vs C++ (first comment). Not about accessing COM from C vs C++ – Χpẘ Mar 11 '16 at 21:27
  • Hmm, okay. I can think of ways that could be problematic, but I suppose it works in a controlled environment. Different question, then: why run it as a service? Just launch it at startup when a user logs in. – Cody Gray - on strike Mar 12 '16 at 06:07
  • @CodyGray Mostly it's because we have the same code base for Linux and Windows, and for some reason the guy that implemented the Windows part of the code chose service. I am lobying for a change to a classic application, so that it can have a tray icon, which would be even better for the user, but for now I just have to finish this. Seems like I will keep it per user with the REG hack, which will work ok after the rewrite to a startup application. – Spadeas Mar 12 '16 at 10:02

0 Answers0