Click here to Skip to main content
Click here to Skip to main content

An explorative peek into generating deterministic primes through a multi-level multi-language tunneling apparatus

By , , , 2 Apr 2010
 

This article was written as a joke for April Fools Day 2010. Readers are advised not to take the contents of this article too seriously.

Introduction

This paper is a summary of a research exercise conducted in conjunction by Nish Sivakumar, Professor Cuthbert Calculus, and Glenn Quagmire. The paper describes an explorative peek into generating deterministic primes through a multi-level multi-language tunneling apparatus. The example code is kept simple enough so as to keep focus on the fundamental problem we are attempting to solve. At the core, we start off with an inline assembler method consumed by an exported C function, wrapped and exported as a C++ class that's then consumed and exposed to the CLR through a C++/CLI wrapper, which is then used by a C# COM DLL that exposes an interface that is consumed through VBScript and executed through a command scriplet. 

Inline assembly

int GenerateIndex(int num)
{
  _asm
  {
    mov eax, dword ptr [num]
    xor edx, edx
    mov ecx, 3
    idiv ecx
    mov eax, edx
  }

This is the core randomization code and it's in assembly to keep things simple and lightweight. In the example we restrict the number of cached prime numbers to 3 and hence the divisor in the code.

C DLL Export

int __stdcall DllMain()
{ 
  return 0;
}

int primes[] = { 29, 37, 61 };

__declspec(dllexport) int GetDetPrime()
{
  return primes[GenerateIndex(GetTickCount())];
}

The C DLL essentially exports a simple function that returns a deterministic prime, and the code internally delegates the randomization logic to the assembler code. At this point the library is consumable by any framework or layer capable of calling a C DLL.

C++ class Export

// The header file
class __declspec(dllexport) CDetPrimeLib 
{
public:
  int GetDetPrime();
};

// Implementation file
#pragma comment(lib, "DetPrime.lib")

extern "C" __declspec(dllimport) int GetDetPrime();

int CDetPrimeLib::GetDetPrime()
{
  return ::GetDetPrime();
}

Here we wrap the C DLL and export it as a C++ class to natively support the most popular programming language in the world. It's a very thin wrapper since we don't want to add any overhead at this point.

CLI wrapper using C++/CLI

#pragma once

#pragma comment(lib, "DetPrimeLib.lib")

class __declspec(dllimport) CDetPrimeLib 
{
public:
  int GetDetPrime();
};

namespace DetPrimeManWrap 
{
  public ref class DetPrimeManaged
  {
    CDetPrimeLib* pNative;

  public:
    DetPrimeManaged()
    {
      pNative = new CDetPrimeLib();
    }

    ~DetPrimeManaged()
    {
      this->!DetPrimeManaged();
    }

    !DetPrimeManaged()
    {
      delete pNative;
    }

    int GetDetPrime()
    {
      return pNative->GetDetPrime();
    }
  };
}

While we have already provided enough API exposure to be considered mainstream, it makes good sense to also directly support .NET and thus we have a C++/CLI wrapper library that exposes the functionality to managed callers.

COM wrapper using C#

[assembly: ComVisible(true)]
[assembly: Guid("6b4bf847-a45d-4f87-ba53-0d4a9fffa975")]
[assembly: AssemblyVersion("1.0.1.1")]
[assembly: AssemblyFileVersion("1.0.1.1")]

namespace DetPrimeCOM
{
    using DetPrimeManWrap;

    [Guid("18535B5E-0561-4BA8-8CF6-85B148F3A4CF")]
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IDetPrime
    {
        int GetDetPrime();
    }

    [Guid("2299461C-1FFB-4D5D-A521-0830F93FB5CC")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class DetPrime : IDetPrime
    {
        public int GetDetPrime()
        {
            return new DetPrimeManaged().GetDetPrime();
        }
    }
}

Since we support .NET we may as well support COM too and that's what the above C# code does. The code is now ready for COM consumption.

VBScript caller

Dim detPrime 
Set detPrime = CreateObject("DetPrimeCOM.DetPrime")
Wscript.Echo "The generated Deterministic Prime is " & detPrime.GetDetPrime

VB6 is now officially extinct and so we resort to a simple VBScript script to invoke the COM library.

Command scriplet

@%windir%\syswow64\cscript.exe /nologo DetPrime.vbs

And the last piece in our architecture is the command scriplet which executes the VBScript through cscript.exe so we can run it on a 64 bit OS, even though the code is 32 bit to support the vast majority of frameworks out there.

Conclusion

This article was written as a joke for April Fools Day 2010. Readers are advised not to take the contents of this article too seriously.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Authors

Nish Sivakumar
United States United States
Member
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
 
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com.
 
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
 
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
 
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Professor Cuthbert Calculus
Architect
United States United States
Member
I am a multiple PhD with a deep interest in science. My recent research has been in the field of deterministic prime numbers.

Glenn Quagmire
Program Manager
United States United States
Member
I am a former airline pilot who's currently doing research on deterministic primes under Professor Calculus.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membergndnet7 Nov '12 - 7:44 
GeneralMy Vote of 5mvpthatraja1 Apr '11 - 5:44 
GeneralRe: My Vote of 5mvpNishant Sivakumar1 Apr '11 - 9:29 
JokeExcellent articlemvpthatraja14 Jan '11 - 3:40 
GeneralRe: Excellent articlemvpNishant Sivakumar14 Jan '11 - 3:53 
GeneralNever tell the same joke twicememberpwasser22 Dec '10 - 16:25 
GeneralWhoasitebuilderAndy Brummer2 Apr '10 - 5:13 
GeneralRe: WhoamvpNishant Sivakumar2 Apr '10 - 5:22 
GeneralRe: WhoamvpPete O'Hanlon2 Apr '10 - 9:09 
GeneralPlzzzz HelpmemberAbhinav S2 Apr '10 - 4:45 
GeneralRe: Plzzzz HelpmvpNishant Sivakumar2 Apr '10 - 4:46 
GeneralRe: Plzzzz HelpmemberAbhinav S2 Apr '10 - 5:16 
GeneralNice - but impressive !memberemilio_grv1 Apr '10 - 21:22 
GeneralRe: Nice - but impressive !mvpNishant Sivakumar2 Apr '10 - 2:02 
GeneralThis could lead to man love.mvpPete O'Hanlon1 Apr '10 - 9:27 
GeneralRe: This could lead to man love.mvpNishant Sivakumar1 Apr '10 - 12:34 
GeneralExcellentmemberDouglas Troy1 Apr '10 - 9:16 
GeneralRe: ExcellentmvpNishant Sivakumar1 Apr '10 - 12:34 
GeneralMemory hog...memberMike Rich1 Apr '10 - 8:37 
GeneralRe: Memory hog...mvpNishant Sivakumar1 Apr '10 - 12:33 
GeneralBrilliant stuff!memberRavi Bhavnani1 Apr '10 - 7:32 
GeneralRe: Brilliant stuff!mvpNishant Sivakumar1 Apr '10 - 12:33 
GeneralMy vote of 1membersam.hill1 Apr '10 - 4:54 
GeneralRe: My vote of 1mvpNishant Sivakumar2 Apr '10 - 2:03 
GeneralRe: My vote of 1membersam.hill2 Apr '10 - 18:21 
GeneralRe: My vote of 1mvpNishant Sivakumar3 Apr '10 - 3:19 
GeneralRe: My vote of 1membersam.hill3 Apr '10 - 7:10 
GeneralRe: My vote of 1mvpNishant Sivakumar3 Apr '10 - 7:15 
Generalhave you tried thememberJohnWallis421 Apr '10 - 4:30 
GeneralRe: have you tried thememberGlenn Quagmire1 Apr '10 - 4:37 
GeneralRe: have you tried thememberJohnWallis421 Apr '10 - 14:59 
GeneralWow, drilling layers of stuff!mvpCPallini1 Apr '10 - 3:18 
GeneralRe: Wow, drilling layers of stuff!memberProfessor Cuthbert Calculus1 Apr '10 - 4:35 
GeneralHi TrifoniusmvpLuc Pattyn1 Apr '10 - 3:14 
GeneralRe: Hi TrifoniusmemberProfessor Cuthbert Calculus1 Apr '10 - 4:35 
GeneralFreekin ace manmvpSacha Barber1 Apr '10 - 2:14 
GeneralRe: Freekin ace manmvpNishant Sivakumar1 Apr '10 - 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 2 Apr 2010
Article Copyright 2010 by Nish Sivakumar, Professor Cuthbert Calculus, Glenn Quagmire
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid