Thursday, 15 May 2014

Creating C/C++ Dynamic Link Library (C/C++ DLL) Using Visual Studio

I was working on a project to create a C dynamic link library (.DLL). It seems difficult at the beginning. However, after several attempts, manage to produce it.

For creation of C++ library you can refer to the MSDN. I'm following these steps and enhance on the same codes to produce C DLL.

  1. In order to access a library file, we can utilize the Dependency Walker.
  2. The library that generated by the sample code by MSDN is as picture below. Take note of the difference between C++ functions and C functions.
  3. To export C functions, we can the export function names in .def file
  4. Header file will look something like this. The function for the Add is normal declaration in Header file.

  5. The implementation code is a standard code.
    1. #include "stdafx.h"

    2. #include "CryptoCPPDLL.h"
    3. #include <stdexcept>

    4. using namespace std;

    5. namespace CryptoFuncs
    6. {
    7.     double CryptoCPPDLL::Add(double a, double b)
    8.     {
    9.         return a + b;
    10.     }

    11.     double CryptoCPPDLL::Subtract(double a, double b)
    12.     {
    13.         return a - b;
    14.     }

    15.     double CryptoCPPDLL::Multiply(double a, double b)
    16.     {
    17.         return a * b;
    18.     }

    19.     double CryptoCPPDLL::Divide(double a, double b)
    20.     {
    21.         if (b == 0)
    22.         {
    23.             throw invalid_argument("b cannot be zero!");
    24.         }

    25.         return a / b;
    26.     }

    27. double CryptoCPPDLL::Add2(double a, double b)
    28.     {
    29.         return a + b;
    30.     }

    31.     double CryptoCPPDLL::Subtract2(double a, double b)
    32.     {
    33.         return a - b;
    34.     }

    35.     double CryptoCPPDLL::Multiply2(double a, double b)
    36.     {
    37.         return a * b;
    38.     }

    39.     double CryptoCPPDLL::Divide2(double a, double b)
    40.     {
    41.         if (b == 0)
    42.         {
    43.             throw invalid_argument("b cannot be zero!");
    44.         }

    45.         return a / b;
    46.     }
    47. void CryptoCPPDLL::PassStructIn(MyStruct* myStruct)
    48. {
    49.       if (myStruct != NULL)
    50.       {

    51.             myStruct->SomeId = 234;
    52.             myStruct->SomePrice = 456.23;
    53.       }
    54. }
    55. void CryptoCPPDLL::PassStructIn2(MyStruct* myStruct)
    56. {
    57.       if (myStruct != NULL)
    58.       {

    59.             myStruct->SomeId = 234;
    60.             myStruct->SomePrice = 456.23;
    61.       }
    62. }

    63. void PassStructIn3(MyStruct* myStruct)
    64. {
    65.       if (myStruct != NULL)
    66.       {

    67.             myStruct->SomeId = 234;
    68.             myStruct->SomePrice = 456.23;
    69.       }
    70. }

    71. double Add3(double a, double b)
    72. {
    73. return a + b;
    74. }

    75. }
  6. The second way to do this is through extern "C" __declspec(dllexport) YOUR_FUNCTION_NAME.
  7. Hope this helps. I will create another post to how to do it in Eclipse using GCC.







Tuesday, 13 May 2014

How To Check Build Status In TFS (Team Foundation Server) From Visual Studio IDE 2012

If your organization is using TFS as source control, build & deployment agent, and you are required to check in codes that you have written, it is important for you to ensure that the codes you have checked in compiled and build successfully by TFS Build Controller.

It's pretty simple steps to check it.

** I assume your Visual Studio IDE already configured to connect to the TFS server in your organization.

2.



Tuesday, 15 April 2014

RangeValidator Control Date Checking Error


Server Error in '/' Application.


 

The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 


[HttpException (0x80004005): The value '15/04/2024' of the MaximumValue property of 'RangeValidator2' cannot be converted to type 'Date'.]
   System.Web.UI.WebControls.RangeValidator.ValidateValues() +1482938
   System.Web.UI.WebControls.RangeValidator.ControlPropertiesValid() +12
   System.Web.UI.WebControls.BaseValidator.OnPreRender(EventArgs e) +36
   System.Web.UI.Control.PreRenderRecursiveInternal() +83
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Control.PreRenderRecursiveInternal() +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +974
 
I have encountered this problem when try to use range validator control.
The reason is because the server date locale setting is "mm-dd-yyyy" (en-US) format. But I'm trying to assign "wrong" value "15/04/2004" (which in my application, i'm using dd/mm/yyyy format across all my date value) into the range validator maximum value.
To overcome this problem, these are what i do.