Code Repo    |     RSS
MD's Technical Sharing



Monday, July 28, 2008

Writing OneNote toolbar add-in

Reference: Ccreating Toolbar Buttons in OneNote 2007,

We allow you to create a toolbar button which is either an icon or text that will sit on one of the OneNote command bars. They can appear on the standard toolbar at the end of the menus or on any other toolbar. When the user clicks on the button we will CoCreate your code (since you implement an interface we make available) and we send you the current page that was sent. You can see that it looks like this:

Download sample code from OneNote Web Exporter http://www.codeplex.com/ONWebber. The solution contains

  • a C# console application project called "cwebber".
  • a C# class library project called "Webber".
  • a setup project called "Webber Setup".


Webber Setup will install cwebber and create its Start Menu shortcut and register Webber as a OneNote add-in. To debug Webber:
  1. Build Webber Setup and run the .msi to setup the add in
  2. At the beginning of the OnClick event of the add-in (found in Program.cs), put a MessageBox in order to suspend the process when the event has fired, given us enough time to attach the debugger.
  3. In OneNote, click on the add-in icon. The message box will be displayed.
  4. With the MessageBox displayed, go to visual studio, open the Webber solution, choose Debug->Attach to Process and select dllhost.exe (aka COM surrogate), which is the process responsible for handling the add-in's DLL
  5. Breakpoints will now be hit.


Tip: After you've installed the .msi for the OneNote add-in, on some subsequent changes to the code, you don't have to uninstall and re-install the msi. Just quit OneNote, ensure that the project properties for webber have been set to "Register for COM interop", build it, and the changes will be reflected.
Read More »

Thursday, July 24, 2008

.NET send a GET HttpWebRequest and retrieve the server response

'Create the HttpWebRequest object
Dim req as HttpWebRequest = WebRequest.Create(URL)

Try
'Get the data as an HttpWebResponse object
Dim resp as HttpWebResponse = req.GetResponse()

'Convert the data into a string (assumes that you are requesting text)
Dim sr as New StreamReader(resp.GetResponseStream())
Dim results as String = sr.ReadToEnd()
sr.Close()

'... work with results ...
Catch wex as WebException
'Something went awry in the HTTP request!
End Try
Read More »

Wednesday, July 23, 2008

Manually create a new MySQL user account

1. Log on to MySQL as root.
2. Grant permission:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'@'localhost'
-> IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
3. If encountered error 'Client does not support authentication protocol requested by server; consider upgrading MySQL client', try using
mysql> SET PASSWORD FOR
-> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');
Read More »

Sunday, July 20, 2008

MAC OS System 8.0 on Basilisk

UPDATE: For a list of interesting old Mac applications which you can play with using Mini vMac, Basilisk and SheepShaver, refer to my latest article.

This guide shows you how to run MAC OS 8 on the Basilisk emulator. This is the last version of Mac OS X that can be run on this emulator. The emulator emulates a 68k Macintosh, while later versions Mac OS requires a PowerPC.

It is recommended that Windows XP SP2 or older is used. Certain part of Basilisk is not compatible with Windows Vista or Windows 7.

First download Basilisk from here

1. Configure CD-ROM driver: copy CDENABLE.vxd or CDENABLE.sys (depending on Windows version) from the CD-ROM drivers to c:\windows\system32. A re-boot may be needed.
2. Installation of network card (recommended if you want Networking such as Internet)

      - Log in with administrator rights. After installation, user rights suffice.

      - Open Network and Dial-up Connections in Control Panel.

      - Click Local Area Connection (or the active connection to the Internet)

      - Click the File menu, and then click Properties, or click the Properties button.

      - In the Local Area Connection Properties dialog box, click Install.

      - Select "Protocol", and then click "Add"

      - Click "Have disk".

      - Browse to the folder where you unzipped files "B2Win2k.inf" and the Windows 2000 version of the "B2ETHER.SYS" driver. Click OK.

      - Select "Basilisk II Ethernet driver" from the list and click OK.

      - Close the dialog. Wait until the binding analysis is complete,

      - Reboot the computer (recommended).

      - After reboot, select the desired network card in the Basilisk GUI ethernet page.

3. Start BasiliskIIGUI.exe and perform the configuration as per the screenshot

Tab Memory - load a compatible ROM. Some ROMs (both Old World and New World) can be downloaded here. You can use the QUADRA650.ROM



Tab Disk: Load the empty hard disk image into the Installed Disk panel. The disk should be created previously using HFVExplorer.


Depending on the type of the installation CD image, you may be able to mount it directly in the Disk tab or you have to mount it via a separate tool such as DAEMON tools and configure it in the CD tab.

If the installation media is on CD, configure it to boot from the emulated CD-ROM. Else choose "boot from first bootable media"


4. Now double-click BasiliskII.exe to start the emulated Mac. By default it will start in full-screen. You can also configure a custom screen resolution in the Screen tab. Press Alt-Enter to switch between full-screen and windowed mode. Double click on Mac OS Install icon to start the installation.

5. Follow the on screen instruction and continue installing. When completed, shut down the Mac, open Basilisk configuration panel again and remove the emulated CD-ROM. Configure the Mac to boot from the first bootable volume and you'll be able to boot up Mac OS 8



Basilisk can only run up to System 8. To run up to System 9.0.4, you'll need SheepShaver. Detailed instructions are available here.

On a side note, System 9.1 is available as free update for System 9.0.4 users. However, it cannot be installed on the Mac emulated by SheepShaver. If you attempt to do so, the installation will continue flawlessly but the system won't be able to boot after a restart. The error message says "Incompatible processor". In fact, System 9.0.4 is the last version of classic Mac OS that can be emulated on a PC. The newer PearPC emulator can only support OS X onwards.

Also, if SheepShaver is running but not having focus (e.g. you run SheepShaver in the background while using other applications), very often the mouse cursor would move to the top left of the screen. There is no known fix for this.

Read More »

Wednesday, July 16, 2008

Connect to MS SQL database from ASP/ASPX

In this tutorial we will show you how to connect to MS SQL Server database. Everything is commented so you won't have trouble.

Connect with a DSN from ASP

DSN stands for 'Data Source Name'. It is an easy way to assign useful and easily rememberable names to data sources which may not be limited to databases alone. If you do not know how to set up a system DSN read our tutorial How to set up a system DSN.

<%
'declare the variables
Dim Connection
Dim Recordset
Dim SQL

'declare the SQL statement that will query the database
SQL = "SELECT * FROM TABLE_NAME"a

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'open the connection to the database
Connection.Open "DSN=dsn_name;UID=user_name;PWD=password;Database=database_name"

'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof
Response.write Recordset("FIRST_FIELD_NAME")
Response.write Recordset("SECOND_FIELD_NAME")
Response.write Recordset("THIRD_FIELD_NAME")
Response.write "<br>"
Recordset.MoveNext
Loop
End If

'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>

Connect without a DSN (using a connection string) from ASP

Let’s look at a sample script to get an idea how to connect to MS SQL Server database without DSN:

<%
'declare the variables
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL

'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=yourServername;UID=yourUsername;" & _
"PWD=yourPassword;DATABASE=yourDatabasename"

'declare the SQL statement that will query the database
SQL = "SELECT * FROM TABLE_NAME"

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString

'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

'first of all determine whether there are any records
If Recordset.EOF Then
Response.Write("No records returned.")
Else
'if there are records then loop through the fields
Do While NOT Recordset.Eof
Response.write Recordset("FIRST_FILD_NAME")
Response.write Recordset("SECOND_FIELD_NAME")
Response.write Recordset("THIRD_FIELD_NAME")
Response.write "<br>"
Recordset.MoveNext
Loop
End If

'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>

Connect from ASP.NET page

<%@ Import Namespace="System.Data.SqlClient" %>
<%
Dim conDBID As SqlConnection
Dim cmdSelectTABLE_NAME As SqlCommand
Dim dtrTABLE_NAME As SqlDataReader
conDBID = New SqlConnection( "Server=mssqlhostsqlexpress;uid=USERNAME;pwd=PASSWORD;database=DBID" )
conDBID.Open()
cmdSelectTABLE_NAME = New SqlCommand( "Select COLLUMN_NAME From TABLE_NAME", condDBID)
dtrTABLE_NAME = cmdSelectTABLE_NAME.ExecuteReader()
While dtrTABLE_NAME.Read()
Response.Write(“<li>" )
Response.Write(dtrAuthors( "COLUMN_NAME" ) )
End While
dtrTABLE_NAME.Close()
conDBID.Close()
%>

Read More »

Sunday, July 13, 2008

MyODBC - Connect to MySQL from ASP/ASPX

MyODBC is a system driver which is required if you want to connect to a MySQL database from an Active Serve Page (ASP) document, or any other type of server pages such as PHP or JSP.

Firstly run the setup program. After the installation finishes, open Control Panel/Administrative Tools/Data Sources. Click Add Data and select MySQL x.xx add the end of the list.

Enter the data source name (to some extent, this can be any name you like; as the name will not affect the ASP script) and type the username and password.

Click OK to close the dialog box.

A sample code to open a MySQL Database from an ASP file is shown below

<%
Dim sConnection, objConn , objRS

sConnection = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost; DATABASE=database_name; UID=username;PASSWORD=password; OPTION=3"

Set objConn = Server.CreateObject("ADODB.Connection")

objConn.Open(sConnection)

Set objRS = objConn.Execute("SELECT username, realname FROM accounts")

While Not objRS.EOF
Response.Write objRS.Fields("username") & ", " & objRS.Fields("realname") & "<br>"
Response.Write " "
objRS.MoveNext
Wend

objRS.Close
Set objRS = Nothing
objConn.Close
Set objConn = Nothing
%>

An explanation of the OPTION parameter is found below

Configuration - Option Value
Microsoft Access - 3
Microsoft Visual Basic - 3
Large tables with too many rows - 2049
Driver trace generation (Debug mode) - 4
Query log generation (Debug mode) - 524288
Generate driver trace as well as query log (Debug mode) - 524292
Large tables with no-cache results - 3145731

For more information, visit
Read More »

Saturday, July 12, 2008

Display a dialog box retrieved from resource

To display a dialog created via Visual Studio Resource editor, use the DialogBox function. Assuming the dialog box resource name is IDD_DIALOG1:

int dlgResult = DialogBox(g_hinstModule, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, (DLGPROC) DlgCallbackProc);

When an event occurs in a dialog box control, the control sends a WM_COMMAND message to the dialog box procedure. The high-order word of the wParam parameter is a notification code, indicating the type of event that occurred. The low-order word of wParam is a constant identifying the control. The lParam parameter is the window handle for the control.

To close the dialog box, call EndDialog. The second parameter of EndDialog will be the return value of the blocking call to DialogBoxW, which is dlgResult in the above code.

BOOL CALLBACK DlgCallbackProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
//occurs when dialog box was initialized

case WM_COMMAND:
//when a control receives action
switch (LOWORD(wParam)) //ID of the control
{
case IDR_MENUITEM1:
//a menu item was pressed. Pass IDR_MENUITEM1 to indicate that the ACCEPT menu has been clicked
EndDialog(hwndDlg, IDR_MENUITEM1);
break;
}

break;
}
return FALSE;
}

g_hinstModule is the handle to the executable containing the resource.

Reference:
http://win32.freetechsecrets.com/WIN32Processing_the_WMCOMMAND_Message.htm
Read More »