Monday, August 30, 2010

Missing Import Library .LIB File

Today i had a problem when i wanted to implicitly link my program with a function residing in ntdll.dll and since i have Microsoft Visual C++ 6.0, i couldn't find the corresponding import library, ntdll.lib, nor the header file. And for some reason, I didn't want to explicitly link to ntdll.dll.

So i thought about doing this without using any external tools. I don't know if any tool does really exist for this.

I had any idea to achieve this. All we have to know is the function name, calling convention, and arguments types and number. Even if we miss one of them, it is not the end of world, there are other solutions that i will blog about in later posts.

The solution is as follows:

Just act like you are creating a DLL from the scratch. Open a project for dynamic-link library. Give it the same name as the DLL you want to implicitly link to.

Now create a dummy function with the same name as the function we want to import. Also give it the same calling convention and arguments info.


To implicitly link to ntdll.dll and import the "wcslen" function, Do as in the image below:_


As you can see our wcslen does nothing, it is just a skeleton for the real "wcslen" function.

Now export the dummy function from the dummy DLL by creating a .def file (the way i prefer).



After we have successfully built it, we get  ntdll.dll and ntdll.LIB. we only care about ntdll.LIB. Copy it into the "microsoft visual studio\VC98\Lib" folder.


Back to our original project, add ntdll.LIB to your project's Object/Library modules.

Now you have to write the function prototype:

extern "C"
{
_declspec(dllimport) int _cdecl wcslen(wchar_t* );
}

Finally we can  use the "wcslen" function this way:

int main(int argc,char* argv[])
{
wchar_t* p=L"walied";
return wcslen(p);
}

Any questions or suggestions are welcome.

Thursday, August 26, 2010

Simple Random Number Generator

Today i decided to write my own rand function to generate a random number without any  use of the time APIs. Unlike the famous "rand" function, the "myrand" function doesn't require any srand function calls.

My function depends on an x-86 instruction called RDTSC. This instruction reads the processor time stamp i.e. the number of clock cycles since the last reset. The time stamp is read into the  EAX:EDX registers.

The "myrand" function takes only one parameter called X e.g. if X is 5, the generated random number will range from 0 to 4.

The code is so simple that it can be implemented in few lines.


int myrand(int X)
{
          int a;
         __asm
       {
                pushad
                rdtsc
               mov a,eax
               popad
       }
       return a%X;
}

The cons. for this function is that there is no guarantee that the target processor supports it, but i see it as a good step towards writing my self-implemented function.

Any ideas or comments are very welcome.

Sunday, August 15, 2010

Simple Permutations Algorithm

Today i decided to write my own algorithm for generating permutations for a given set of integers. One of the ideas that hit my mind was using array cyclic shift.

The algorithm is as follows:_

for 1 2 3 4
it would be like this
1 2 3 4 --->2341-->3412-->4123

For each set of the above 4 sets, the element with index 0 is fixed and the rest of the array is shifted. In this manner:

1234-->1342-->1423

For each one of the above 3 sets, the elements with indexes 0 and 1 are fixed  and the rest of the array is shifted.
In this manner:

1234-->1243

Here is the code for my algorithm

void permutation(int X[],int size,int level)
{
//----------------------------
if(level==size-1) return;
//----------------------------
int* A=(int*)malloc(size*sizeof(int));
memcpy(A,X,size*sizeof(int));
//----------------------------
int i=0;
while(i<(size-level)) { shift(&A[level],size-level,i); if(!level) print(A,size); else if(i) print(A,size); permutation(A,size,level+1); i++; memcpy(A,X,size*sizeof(int)); } free(A); return; }

Any suggestions are very welcome.  You can find an example here.