Need to be able to a DLL (how/template to use)

Posted on 2006-06-01
Last Modified: 2010-04-15
I use the R&R (ReportWorks) report writer.  It has the ability to call a DLL, specify the functions and then pass a 8000 character buffer, returning the modified buffer and a T/F value as the return value.

I don't program in C and am looking for a template and instructions that I can use as a basis for creating a dll collection of some additional functions not available in R&R.  For example, do while/for next loops, building encryption routines, etc.

I have access to visual C Version 6 from Microsoft.

Does anyone have something that I can use as a template to learn how to start doing this.

The DLL would be a dynamic DLL loaded at runtime.  Please be thorough in your answer as I don't current program in C but can learn what is needed.  There would be no data access, only the passing of the 8000 character string buffer, parsing of parameters from the buffer, performance of string/math calculations, packing the results in the buffer and setting the return value.


Question by:cwsoft05

    Author Comment

    For a simple example,  pass a string in the following format.


    Parse the value, this one has 3 but could have any number of string values, 0000110,00  0000120.00 0000130.00, convert to floating point numbers, add the 3 together together and divide by the number of values (i.e. compute the average) and put the average back into the string, and set the return value to .T.

    Have a 2nd function in the same DLL that will return the largest value (max).

    Thanks,  Cliff.
    LVL 48

    Accepted Solution

    Open Visual Studio 6, select File - New - Project. Select Win32 Dll project type. On one of Project Wizard steps select "Dll which exports symbols".
    When project is created, you can see that Wizard generated for you sample exported class, variable and function. Remove class and variable and use function. You can change function name and parameters according to your needs, add implementation code etc. New functions are added using the same syntax.
    Dll create by Wizard contains h-file with function prototype, and cpp file with function implementation. Wnen you chane function and add new function, make changes in both files.

    Author Comment

    Thanks for the info, any help taking the actual example into some source code.  It would help understanding the C approach.
    LVL 24

    Assisted Solution

    I have no access to the given software, the only thing I can help you with is, how to build a DLL, someone else has pointed out to you what you have to do.

    You just have to see that you "decorate" your exported functions either with

    __declspec(dllexport) fun_return_value name_of_function(par1_type par1, par2_type par2 ) etc.

    or that you write a .def file.

    The information on how to build a DLL can be found in the MSDN documentation.

    I posted an example on how to build and use a DLL here:

    That code will run with MSVC also.

    Author Comment

    Thanks for the information.  I will ook at the above post.  

    Why do you comment "I have no access to the given software"  I provided a sample in the 2nd post of what I was looking for.  This is application independent and does not need access to any software, other than for testing.

    I don't program in C and wanted to limit how much time I need to spend learning to accomplish this little task.

    The specs were simple.

    Pass 8000 character string.
    Parse the 3 strings, (i.e. until a comma or space) was encounterd
    Convert the strings to a numeric value.
    Add the 3 numbers together
    Divide the intermediate result by the number of values parsed (i.e. don't use an average function)
    Convert the result back to a string and pad to 8000 characters
    put the 8000 character string back into the return buffer (same as the input buffer)
    set the function return value to .T.
    exit from the function.

    The above is application indendent.

    LVL 24

    Expert Comment

    I just mentioned that I have not access to ReportWorks, of course I can build a DLL and yes it's qutie easy to write the function you are asking for, but I don't feel I have to do that for free.

    The application is clear to me and I can give you a helping hand, but I won't write that all for you. There are just a few things you do not have mentioned:
    - what happens if there are less then three numbers in the input buffer?

    the prototyp for you functions should be

    __declspec(dllexport) BOOL calculate_average_of_numbers_in (char * buf, size_t len_of_buf) {
    For parsing the character in that function you can use
    - strtok (to split the strign at , or ' '
    - of you can use  prefabricated str_split function
    - the conversion from char * to int can be done this way
    can use sscanf (buf, "%d", &val) , or you can use the atol function for that
    - adding is simply using +
    - dividing is simply by using / (you probably have to use the number as a floating point number of a double
    - converting back the result to a string is just
    a sprintf(buf, "%d", num)

    So that's all there is to do.



    Author Comment

    Thanks for the information.  Experts Exchange does have a monthly cost to it so I don't consider that I am getting anything free from my point of view.  I am used to the community of assistance I receive from Xbase++ users.  They are more than willing to provide sample code for a simple function.  I have helped many users in the areas where I have expertise, but I don't have the same expertise in C,C++,etc.

    As for examples, I have seen many lengthy examples provided in this forum so I did not thing I was asking for anything unusual or lengthy.  If I was asking more than I should that is my mistake, but the request appear reasonable.
    LVL 24

    Expert Comment

    It is I do not want to give you the impression you did not. But one has to sit down and writ the program the usual way you find code here is:
    - someone posts code (that should be the OP)
    - experts comment on it and change it

    If the experts happens to have some code at hand he/she sometimes posted it.

    However all the experts to that on their "free" time. So if you expect an pre-fabricated example just to use then thie migh or might happen.

    Sometimes we sit down and writ the code from scratch, but there's no special incentive to do so also.

    So here we go with the base code (minus every error handling for you request)
    I won't build a DLL because that is not really hard to do:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>

    _Bool calculate_average (char * buf, size_t size_of_buf){
      long val1, val2, val3;
      double result;
      if (3 != sscanf(buf, "%ld %ld %ld", &val1, &val2, &val3)) {
        fprintf(stderr, "not a valid line giving up\n");
        return false;
      result = (double) val1 + val2 + val3;
      result /= 3;
      /* clear out the buf */
      memset(buf, 0, size_of_buf);
      sprintf(buf, "%g", result);
      return true;

    static char line[8000];

    int main (void){
      strcpy(line, "1 2 3");
      calculate_average(line, sizeof(line));
      printf("average of 1 2 3 = %s\n", line);
      return 0;

    Now I have give you all you need to do the rest yourself.


    Author Comment

    Thank you.  If I had code, I would have posted it.  Thanks again very much for your efforts.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Adjust Mfcapp 29 143
    C dll call freezes 5 81
    c++ substatte a varabe for a string in a LPCTSTR statment 8 68
    Problem to scan all sheets 3 68
    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now