Saturday, 24 January 2015

Map network share to a drive letter, permanently and reliably.


I often want to access a shared network folder as at drive letter. Using "subst", "net use", "visual subst" have given variable results. Often a drive letter would be randomly missing after a reboot, not show up in admin mode or kick in too late resulting in "drive X doesn't exist" style messages.

A google on the subject highlighted the DosDevice registry method.

http://en.wikipedia.org/wiki/SUBST#Method_3

It didn't seem clear on its use in network shares. So after a lot of experimentation I found something that worked.

REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices]
"X:"="\\??\\UNC\\NetworkServer\\SharedFolder"

Copy the above to a .reg file, replace "NetworkServer", "SharedFolder" with your needs and run the reg.

Reboot and you are sorted. It will set up the drive letter on each reboot before anything gets launched. Both admin and user will see the drive letter. Bliss!

To verify, bring up a cmd prompt, enter "subst" and you should see something like:
X:\: => UNC\NetworkServer\SharedFolder

Wednesday, 26 June 2013

Windows context menu fragments left on desktop

Selected Item in Context Menu stays visible forever. I right click, up pops the context menu, I select a menu item, the context menu disappears, but leaves the selected menu item permanently visible on top of all other desktop windows. Annoying!! I've had this occur a lot in XP and now Win7 too. Finally I find a solution.. Kill the Desktop Window Manager (dwm.exe) in the task manager. It will restart itself and the zombified menu item will disappear.

UPDATE: 

    Alternatively, and much safer.. Set the desktop bit depth to a different bit depth (16bit), then select NO when asked to keep the setting. The screen gets refreshed, orphan elements disappear. Simple and effective.

UPDATE:

..but not permanent.

Maybe more luck with this..


Nope, fragments just reappear soon enough. Grr..


Friday, 14 June 2013

Symlinks in perforce in windows

Basically, if you are getting small files where symlinks should be, run perforce as administrator. Fuller description here : http://answers.perforce.com/articles/KB_Article/Enabling-Symlinks-on-Windows

Thursday, 30 May 2013

Visual Studio 2012 - "Project file '' has been renamed or is no longer in the solution"

Sometimes when messing about with projects, this unhelpful popup will plague you.

To get some more info about the actual cause..

1. Build this - https://github.com/CoherentLabs/DumpReferencesAddIn

2. Create C:\Users\\Documents\Visual Studio 2012\Addins 

3. Copy in there DumpReferencesAddIn.dll which you built and DumpReferencesAddIn.AddIn which you didn't. 

4. Restart VS2012, load your troublesome solution. 

5. Right click on the solution and select "Dump References". 

6. Enjoy.

Saturday, 18 May 2013

Use a custom icon in a dialog box in windows

When creating a windows dialog, the icon in the top left corner is the dull standard one. Add your own funky alternative by adding this code! SendMessage(mainDialog,WM_SETICON,ICON_BIG,(LPARAM)::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YOURICON))); SendMessage(mainDialog,WM_SETICON,ICON_SMALL,(LPARAM)::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YOURICON))); SendMessage(mainDialog,WM_SETICON,ICON_SMALL2,(LPARAM)::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_YOURICON)));

Tuesday, 2 April 2013

Running commands after exe exit


I needed a EXE to call it's related uninstall.exe when it had finished running. There are many solutions to this but I was after something short and sweet.

Here's what I came across.
http://stackoverflow.com/a/3458433/2237946

so modified to my requirements..


std::string g_PostScript;
int main()
{

g_PostScript+="notepad.exe \"hello.txt\" & "; //add a command
g_PostScript+="uninstall.exe & "; //and another one..

//time to exit
ApplyPostScript();
return 0;
}
void ApplyPostScript()
{
if (g_PostScript.length()==0)
return;
g_PostScript="cmd.exe /C ping 1.1.1.1 -n 1 -w 3000 > Nul & "+g_PostScript;
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};

CreateProcess(NULL, (LPSTR)g_PostScript.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}

Monday, 25 March 2013

Why is this header being included?

Often I'll find a visual studio project not compiling as a header file generates an error. Then I realise there's no need for that header to be needed as it's totally unrelated.
So why is it being included?
It can take a lot of guesswork to track down the reason in a large project, but here's a quick method of showing the whole header tree for a cpp.

Add "/ShowIncludes" to the command line options for that cpp. A recursive list is displayed of each included file. Hopefully an unnecessary header file can be identified to enable this tree to be pruned.

Alternatively set the compile option:
http://msdn.microsoft.com/en-us/library/hdkef6tk(v=vs.80).aspx

Wednesday, 6 March 2013

Vijay World: P4EXP Windows Explorer Integration - Problem Solve...

Vijay World: P4EXP Windows Explorer Integration - Problem Solve...: There were lots of issues  and i was struggling to use p4exp which come in bundle with p4v setup. i Couldn't configure it properly for a...

I forget this one every time when installing perforce explorer integration. Duh.

Subst drives only visible to some applications?

Well this had me stumped for a while. I have a Z drive mapped to a folder on C using VisualSubst.

Yet some applications would deny that the Z drive existed. Very odd.

Finally it dawned on me - the applications which could not see Z were running as administrator.

The solution? Run VisualSubst again, this time as administrator and the drive mapping will not be listed.. add it here and voilá. All applications can now see your mapped drive letter.

--


For a better solution, see http://en.wikipedia.org/wiki/SUBST#Method_3

Sunday, 13 January 2013

UPnP - auto port forwarding in C++ for windows

How hard can it be?

I want my windows program to be able to automatically configure the router to forward a port.
I soon find some source code https://compilr.com/tntblow/asdas/upnpnat.cpp
..yes it works!
I eventually need to use this in my program so I incorporate the source into my project, use it, but it doesn't work. "discovery error is Fail to find an UPNP NAT.". Odd.
I last tested the source ages ago while on XP, and now on Win7. I wonder.. I run the program on and old xp laptop and success, UPNP NAT is found, although further errors occur.

Time for another solution..
A-ha, windows includes some UPnP support!
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa382290(v=vs.85).aspx
Erm.. only seems to search for the UPnP device, and it's not clear how to actually configure the thing. Hmm, shelve that..

The search continues. "class PortForward"
http://social.msdn.microsoft.com/Forums/en-GB/netfxnetcom/thread/41701acc-22d5-42d1-87e1-7f4dd5f1b666
Looking promising.. but lots of compile errors to sort out.. hmm

ooh something else, maybe even simpler!
http://forum.emule-project.net/index.php?showtopic=34100

How odd, spmc is always NULL after this..

get_StaticPortMappingCollection(&spmc);


Maybe this is an XP vs Win7 thing.
Well this doesnt look very promising..
http://support.microsoft.com/kb/928233

More searching..
MiniUPnp. Surely..
Oooh there are prebuild win32 exes.. Grab! Run!

C:\MiniUPnP>upnpc-static.exe -a 192.168.0.2 53200 43300 UDP
upnpc : miniupnpc library test client. (c) 2006-2012 Thomas Bernard
Go to http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
for more information.
List of UPNP devices found on the network :
 desc: http://192.168.0.1:49153/gateway.xml
 st: urn:schemas-upnp-org:device:InternetGatewayDevice:1
Socket error: connect, 0
Found a (not connected?) IGD : http://192.168.0.1:49152/upnp/control/WANIPConnection
Trying to continue anyway
Local LAN ip address : 192.168.0.2
Socket error: connect, 0
ExternalIPAddress = (
Socket error: connect, 0
AddPortMapping(43300, 53200, 192.168.0.2) failed with code -3 (UnknownError)
Socket error: connect, 0
GetSpecificPortMappingEntry() failed with code -3 (UnknownError)
InternalIP:Port = ╦t^±╩t☺:_
external (:43300 UDP is redirected to internal ╦t^±╩t☺:_ (duration=╦t┴úð¯H■() 

Yikes. Not good.

I have UPnP enabled in my router, and it list entries there from other members of the LAN, so I would expect the problem lies in the code.

Back to "class PortForward" listed earlier..
Compiles.. run and get:
PortForward::Access Nat->get_StaticPortMappingCollection succeeded, but left Collection null

Same old story, bah!!!

Apps like shareaza must have a go at this, and the source is available!
Running shareaza, viewing log..



[23:45:14] Windows Firewall setup for UPnP succeeded.
[23:45:14] Trying to setup port mappings using NAT UPnP...
[23:45:19] Trying to setup port mappings using Control Point UPnP...
[23:45:19] Accepting incoming TCP connections on 0.0.0.0 port 0.
[23:45:19] Listening for UDP datagrams on 0.0.0.0 port 0.
[23:45:19] Found UPnP device: WANConnectionDevice (ChildLevel: 0, UID: uuid:ab5d9077-0710-4373-a4ea-5192c8781666)
[23:45:29] Traversing the service list of UPnP device failed.


Well that's looking like a non-starter.

Maybe my trusty old netgear DG834G router is only partially UPnP compatible?









Tuesday, 18 October 2011

Why is my application slow?

I've used plenty of CPU / GPU profiling solutions on various platforms before, so when it finally became time to profile a win32 app I had written I looked around for a decent method.
This turned out to be surprisingly hard! Nothing decent was built into Visual Studio professional. I don't have the Team edition, so I cannot comment there, although why such a fundamental thing as a profiler is not present by default is a mystery.
Sure, I could go old skool and fill my code with clock checking around suspect functions, but surely there's a faster way.

VTune - this I just did not get on with. Pricey too. Had to system restore in the end to get my system back up.

AMD CodeAnalyst - promising, but not there yet.

Windows performance analysis tools (Xperf) - I'm happy in a command line environment, but this just got too fiddly.

Very Sleepy - Within one minute of download I had all the bottlenecks in my application in front of me.. even with call stacks. HURRAH!!

...so a mega thumbs up to http://www.codersnotes.com/sleepy
Free too!

and another.. also shows source line. A bit more techy, but still free!
http://lukestackwalker.sourceforge.net/


Monday, 21 March 2011

Windows XP - Cant open applications? Window elements missing?

I've been using XP for years, usually doing 10 things at once, and therefore having plenty of windows open. Eventually I'll open an explorer window and it will look malformed, or I'll run an application and it will start acting very strange.
The solution I've had to employ is to close down some of the lesser-needed windows and then try again, usually with success. It's as if there's a memory leak, but there's no sign of any lack of free memory.
This mystery plagued me for so long until I eventually found the cause and a fix.. An internal memory heap needed to be expanded to accommodate all these windows!

It involves a registry tweak and a reboot, but microsoft have provided a foolproof fix to run, which I used and it's finally removed the only problem I've had with XP. Wish I had found it years ago.. argh!
http://support.microsoft.com/kb/126962 (Scroll down and click on "Fix It")

Install, reboot, open all the windows you want without fear of hitting any hidden limits. (well, eventually maybe!)

Wednesday, 24 November 2010

error C2662: cannot convert 'this' pointer from 'const

error C2662: cannot convert 'this' pointer from 'const

OK I've fallen for this one too many times now! What is means is that you are calling a function on a const object, and the compiler is worried that the function will modify the object.. so assuming it doesnt then add const to the end of the declaration and all will be well in the world again.

class MyClass
{
void Func() const;
}

Monday, 13 September 2010

Getting the time into a filename using a batch file

I'm repeatedly grabbing some data from an external device, and I want it stored locally under a suitable filename which includes the time for reference.


:loop
SET now=%date:~6,4%-%date:~3,2%-%date:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%
grabdata.exe data%now%.bin

REM The line below forces a pause for two seconds. Very handy!
@ping 127.0.0.1 -n 2 -w 1000 > nul

goto loop


A quick work of warning - you may need to adjust the parameters of the date to match your location.
when I type at the command prompt
echo %date%
I get
13/09/2010
so be prepared to modify if you don't have DD/MM/YYYY format.




Tuesday, 31 August 2010

Project : error PRJ0003 : Error spawning 'resgen.exe'.

Project : error PRJ0003 : Error spawning 'resgen.exe'.

Kept getting this in vs2008. Searched for resgen.exe and it existed in the windows SDK directory.

So simply go to Tools/Options/ProjectsAndSolutions/VC++ Directories and append "C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin" or whatever to the Executable files list. Sorted!

Saturday, 28 August 2010

System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

Argh! Been trying to install a service for about 2 hours. I followed the wizard in vs2008 to create a new service, added a call to start a thread of my code, hit compile, removed unicode and set to clr, hit compile again, and woo an EXE. Opened a command prompt, typed in "myservice.exe -Install" and oh dear.. failure. Seems I had to right click some crazy stuff and "Add Installer".. Sure I didnt have to do that part last time I made a service. Odd. So did it anyway, and tried again.
Still no luck! Tried creating an ASPNET admin user on my machine.. nope..
Some progress was made when switching properties from Local Service to Local System.. but the big breakthrough came when I closed my command prompt and opened another one as administrator.
Still not convinced by all this admin crud in Win7. Not sure it's an advance on XP in that regard. Ho hum, it looks like I've got a service to play with at last :)

Monday, 23 August 2010

The application has failed to start because its side-by-side configuration is incorrect.

Argh!
I recent spent a very frustrating day trying to get a program that was built with vs2005 running on a pc without visual studio installed. Any pc with visual studio installed had no problem running the simple exe, but without, this error always appeared:
"The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail."
Straight away I meddled with "Multi-threaded / Multi-threaded DLL", but going totally static wasn't possible as some external librarys were being used that insisted on DLL action. So forget the static, and play nice.. Installed vs2005 runtime on the target machine, and vs2005 runtime SP1. Still no luck. I was compiling in XP, and vista was on the target machine. Maybe an issue there? The vista machine probably wasn't up to date with windows updates too.
Anyway, six frustrating hours later, I opened up the project in vs2008 instead, converted it, and soon managed to get it to compile. This worked!
So it looks like I'll be using vs2008 for all my pc projects now.

Wednesday, 7 July 2010

LINK : fatal error LNK1000: Internal error during IncrBuildImage

Oh dear, what's all this?

After a fresh Windows7 install my vs2008 c++ project kept regularly throwing this at me at the link stage.

A microsoft hotfix seems to have sorted it though :

Although maybe the real reason is that I haven't installed the vs2008 SP1 update yet.

Either way the problem has gone so I'm a happy bunny.

Tuesday, 6 July 2010

dxguid.lib(dxguid.obj) : fatal error LNK1103: debugging information corrupt; recompile module

dxguid.lib(dxguid.obj) : fatal error LNK1103: debugging information corrupt; recompile module

Argh, what's going on?! There's surely nothing wrong with the debugging info in dxguid.lib. So what's the real problem?

Well it's a known backward compatibility with the Windows SDK. Using the Windows 7 SDK with Visual Studio 2005 can cause problems if linking with libs created with Visual Studio 2008.

So.. either migrate to VS2008, or install the microsoft hotfix http://support.microsoft.com/kb/949009/

..and you are back in action!!