Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, November 29, 2010

Video Lectures Online - Part 1

Stanford University on YouTube: http://www.youtube.com/user/StanfordUniversity

Massachusetts Institute of Technology on YouTube: http://www.youtube.com/MIT

Indian Institute of Technology on YouTube: http://www.youtube.com/iit#g/p

Wednesday, July 28, 2010

Reversing the Order of Lines in a File


//
// reversor.js
//
// Reverses order of lines in a file
//
// Execute on Windows as:
// cscript reversor.js input.txt > output.txt
// ------------------------------------------------

var args = WScript.Arguments;
if(args.length != 1)
WScript.Quit();

// don't use square brackets below...
// args is not an array/dictionary
var in_file = args(0);

var f;
var lines = new Array();
var fso = new ActiveXObject("Scripting.FileSystemObject");

var i = 0;
var fin = fso.OpenTextFile(in_file);

while(!fin.AtEndOfStream)
lines[i++] = fin.ReadLine();
for(i = lines.length - 1; i >= 0; --i)
WScript.StdOut.WriteLine(lines[i]);

fin.Close();

Wednesday, February 17, 2010

Project with 2 CPP files with the same name in different folders.

This comes as a surprise. I have a C++ project in which the code has been split across several folders. I have two .CPP files of the same name (utility.cpp) in two different folders. The content in the files is not the same. Different symbols defined in each. Sounds like this should work without a hitch, right?

I'm not yet sure of this but it looks like there is no straightforward way to do this in VS2008. Both files are compiled to the same output folder. It looks like the compiler (cl.exe) compiles both .CPP files to the same .OBJ file. So, the last compiled .CPP wins. By the time the linker (linker.exe) reaches the scene, only the symbols defined in the last compiled .CPP are available and the linker fails giving you "LNK2028: unresolved token".

I looked at VS2008 options to see if there was a way to include the relative path of the source file as part of the compiler's output file path, but so far I have not been successful.

Here's the  related post on stackoverflow.com.

Tuesday, March 21, 2006

Accessing Shared Folders in ASP.NET

Believe me its easy. If only you wont get caught up with the 'Impersonation' word. A lot of sites took me for a jolly ride on the Impersonation road. Luckily I found the answer on Experts Exchange.

Here's a demo of the code:


using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Net;

public class ShareThis {
//used in calling WNetAddConnection2[StructLayout (LayoutKind.Sequential)]
public struct NETRESOURCE {
public int dwScope;
public int dwType;
public int dwDisplayType;
public int dwUsage;

[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;

[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;

[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
}

//WIN32API - WNetAddConnection2
[DllImport("mpr.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]
private static extern int WNetAddConnection2A(
[MarshalAs(UnmanagedType.LPArray)]NETRESOURCE[] lpNetResource,
[MarshalAs(UnmanagedType.LPStr)]string lpPassword,
[MarshalAs(UnmanagedType.LPStr)]string lpUserName,
int dwFlags);

//WIN32API - WNetCancelConnection2
[DllImport("mpr.dll",
CharSet = System.Runtime.InteropServices.CharSet.Auto)]

private static extern int WNetCancelConnection2A(
[MarshalAs(UnmanagedType.LPStr)]string lpName, int dwFlags, int fForce);
public static void CopyFile(string share, string username, string password,
string dirFrom, string dirTo, string filename){
NETRESOURCE[] nr = new NETRESOURCE[1];
nr[0].lpRemoteName = share;
nr[0].lpLocalName = "";

//mLocalName;nr[0].dwType = 1;
//disknr[0].dwDisplayType = 0;

nr[0].dwScope = 0; nr[0].dwUsage = 0;
nr[0].lpComment = "";
nr[0].lpProvider = "";

WNetAddConnection2A(nr, password, username, 0);
File.Copy(dirFrom + "\\" + filename, dirTo + "\\" + filename);

WNetCancelConnection2A(share, 0, -1);
}

public static void Main(string[] args) {

CopyFile(@"\\sa191", "username", "password",
@"f:\shared", @"\\sa191\shared", "123.shp");
}
}

About Me

My photo
C/C++ Programmer doing CAD on Windows. Some web development experience. Bangalorean.

Blog Archive

Labels