Friday, October 21, 2005

PDF Files

I found a fantastic class library project to edit and create PDF files its name is "IText_Sharp" which is an import for the original project "IText" from Java to C#, Check it out if you need to edit or create PDF files.
www.sourceforge.net/projects/itextsharp/

Sunday, October 09, 2005

Coding Style

coding style is an important factor in producing an efficient code that can be reused or modified eaisly.

I will review here basic hints in coding styles:
  1. naming variables: best way to name a variable is to choose first an abreviation for it based on its type like "str" for "String" and "num" for "Numebers", then add a descriptive name for the variable ex: if you are declaring a variable for the Monthly Income its name may be like "numMonthlyIncome", take care that to make the variable readable Start every descriptve word in it with a capital letter and the varaiable abbreviation must be in small letters.

  2. naming Functions: try to name Functions so that describe their operation and target and avoid ambigous names, and concentrate on choosing a name that describes the output not the input since the input will be described in the parameteres declaration in the function ex: int GetAverageIncome(string strEmployeeName).

  3. Return Codes: it is a coding style where all the functions in the code always returns an integer value describing the status of the result or any other related message like errors that may occure or confirmation messages or you can combine between the return value of a function and its return codes like in the example of the previous function declaration, we know that an average value of an income must be always a postive value so we can return negative values describing error messages like -1 for "That employee does not exist in the database" and so on.

Monday, October 03, 2005

VBScript and COM (The Array Dilemma) !

its my second day to deal with COM and VBScript and i have faced the problem of passing arrays to and from .NET DLL serving as a COM Component, i described how to do this in the previous post

A problem arises when trying to return an array from a function or pass array to it


How to reproduce:
  • you have a .NET DLL using it as a COM Object
  • you have a function in a class that returns an array of any type
  • you create an instance of this class using CreateObject
  • you call the function that returns an array
  • test the array using IsArray() and VarType() and Ubound()
  • the object is truly an array and its VarType() denots its type and Ubound() gives its valid size
  • then you an item in the array it will give you error "Type Mismatch" !!
Why:
  • VBScript datatypes are all based on a basic type called "Variant" which may corresponds to the "Object" data type in .NET
  • its logic that if VBScript can know the array type which it can access, so why when trying to access a certain item in the array it gives "Type Mismatch" !!!!
  • i think that this is due to a diffrence in implementing and viewing the array from the prespective of .NET and the prespective of VBScript
  • Some solutions on the newsgroups said that you must return your datatype as an Object and this worked in some cases
  • the version of the VBScript processor DLL may have also an influnce in producing this problem

Sunday, October 02, 2005

Calling .NET DLL from Classic ASP Page

I needed to call a .NET DLL from some ASP pages, the DLL was for a search engine and the ASP site was serving as its interface i have done some research and found a way to deal with this the steps are as following:

1- Create a DLL in .NET and be aware that only one class will be used from it (I wasn't able to use any other class than the first one in the dll)

2- build the project

3- Open .NET Command prompt from the tools of .NET located in the Start Menu Shortcuts of .NET

4- Run the following command:
Regasm [Your DLL Path] /codebase

5- in your ASP page create an object from your DLL class
set Inst = Server.CreateObject("NameSpace.ClassName")

replace the NameSpace by your DLL NameSpace and the ClassName by your class name
Note:this code runs in VBScript and it would defer a little if JScript was used

6- You can call a function from your dll using the instance created like:
Inst.CallFunction()

Saturday, October 01, 2005

How to call C or C++ DLL in C#

You can call functions in a dll using the following simple method

1) you must know the function prototype (name of the function and its parameters and its return type).
2) in your code use a code like the following where DllImport Specfies the dll where the function exists and the line under it specifies the function prototype.

using System.Runtime.InteropServices;
namespace myNameSpace
{
public class MyClass
{
[DllImport("DLLPath.DLL")]
public static extern int FunctionName(int Argument1, bool Argument2);
}
}

an Example for using DllImport is to use a Windows API function

we will call a a function that can make you copy a file, its name is CopyFile and it is loacted in Kernel32.dll

this code sample demonstrate how to call this function

using System.Runtime.InteropServices;
namespace FileSystemClasses
{
public class FileSystem
{
[DllImport("Kernel32.DLL")]
public static extern bool CopyFile(string strSourcePath, string strTargetPath, bool bOverwrite);

public void TestDll()
{
FileSystem.CopyFile("c:\\a.txt", "c:\\b.txt", true);
}
}
}

there are other options that can be used with DllImport directive which gives some advanced options and more control which can be explained in another article

For a list of Windows API Functions according to their usage go to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sdk
intro/sdkintro/contents_of_the_platform_sdk.asp