1st post by 1st time C coder

This is a discussion on 1st post by 1st time C coder within the C Programming forums, part of the General Programming Boards category; Hello! I'm Servo Wizard. I'm an accomplished VB6 programmer who is attempting to learn the C-C++ programming language. This project ...

  1. #1
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6

    1st post by 1st time C coder

    Hello! I'm Servo Wizard.

    I'm an accomplished VB6 programmer who is attempting to learn the C-C++ programming language. This project is my very first DLL and it is also the extent of my C programming experience. I proto-typed the functions in a console project and then moved on to the DLL.

    1st major obstacle was figuring out how to return something that VB could digest. There was no way that I know of to export 21 digits other than a string. After 3 days of extensive research I learned how to export a string that VB would accept.

    OK! End of 1st major obstacle, start of 2nd MAJOR OBSTACLE. When called from VB app it returns a string of 21 digits, all being the correct value when split into 7 individual values. I'm calling the DLL from a timer sub in VB and I noticed that the values were not varying as they occasionally should. It did not take to long to find that my string of 21 digits returned as 42 digits, 84 digits, 168 digits and so on until the MS error message arrived. It's quite obvious that the DLL does not close between calls.

    I have spent the entire Labor day weekend trying to learn how to erase, free or whatever method is used to unload the 2 arrays in my DLL so as to prevent the compounding of the export string. Now, I sure that I have looked right at the solution several times but did not recognize it due to my inexperience with the C language.

    It's not my style to have some one else do my learning and thinking for me but I would really appreciate some help with this problem.

    Servo

    Code:
    #include "myheader.h"
    
    char NxtBt[22];
    char Bytes[22];
    
    int  Something;
    
    HANDLE h;
    
    BOOL APIENTRY DllMain
    (
    HANDLE hModule, 
    DWORD  ul_reason_for_call, 
    LPVOID lpReserved
    )
    
    {
    return TRUE;
    }
    
    BSTR __stdcall This()
    {
    if(Something)
      {
      strcat(Bytes, "0");
      }
      itoa(_inp(Blank),NxtBt,10); // decimal value of 4Bh as string "075"
      strcat(Bytes,NxtBt); // array[0]="075"
    
    // repeats the following conditional 5 times with different value (Blank) each time.
    
      if(_inp(Blank)<100)
        {
        strcat(Bytes, "0");
        }
        itoa(_inp(Blank),NxtBt,10);
        strcat(Bytes,NxtBt); // Bytes[0-2]="075";Bytes[3-5]="0??" ; and so on to Bytes[18-21]="0??"
      }
    return SysAllocString((OLECHAR*)Bytes);
    SysFreeString((OLECHAR*)Bytes);
    }
    Last edited by Servo Wizard; 09-04-2007 at 05:23 AM. Reason: clarification

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Some seemingly random code attempting to make a point:
    Code:
    #include <stdio.h>
    
    void foo(int *array, size_t size)
    {
       char text[22];
       size_t i, j = 0;
       for ( i = 0; i < size; ++i )
       {
          j += sprintf(&text[j], "&#37;03d", array[i]);
       }
       puts(text);
    }
    
    int main(void)
    {
       int a[] = {0x4B,1,2,3,4,5,6}, b[] = {1,2,3,4,5,6,7};
       foo(a, sizeof a / sizeof *a); 
       foo(b, sizeof b / sizeof *b); 
       return 0;
    }
    
    /* my output
    075001002003004005006
    001002003004005006007
    */
    Basically I think I'm saying there sounds like an issue with concatenation.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6
    Basically I think I'm saying there sounds like an issue with concatenation.
    Dave,

    Yes, I agree with your synopsis. One of most troubling aspects of C-C++ is the lack of a String type, or my lack of knowledge there of.

    The word string occurs in many C-C++ articles, but I sure don't see the ability to perform any of the string manipulations <string.h> with a 'string'.

    That stated, I realize that VB was wrote in C language so if VB has a String type then C must have the ability to create a String.

    If your code sample equates to a String function then once again I'm missing the point, but like a dog with a bone, I won't let go of it until I've ate all of it, or choked on it.

    Again, thanks for your effort.

    Servo

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I think I was merely recommending to tack a different way in regard to your string creation using C-style arrays. My implementation is not quite the same as yours, so I went a standard route.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Please don't say "C-C++" there are two distinct languages, C and C++ (use a slash instead of a hyphen).
    btw What you have written is entirely C language code, no C++ at all.

    Don't use a variable with the same name as the function please.

    First bug is using strcat on an uninitialised variable. Use strcpy(Blank, "0"); instead.
    Second bug: SysFreeString wont do anything if you put it after the return statement because the function exits before reaching that.
    Third bug: Don't just cast a char* to an OLECHAR* they are not the same. You may need to switch to WCHAR or TCHAR instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This part confuses me:
    Code:
    BSTR __stdcall Blank()
    {
    if(Blank)
      {
      strcat(Bytes, "0");
      }
      itoa(_inp(Blank),NxtBt,10); // decimal value of 4Bh as string "075"
      strcat(Bytes,NxtBt); // array[0]="075"
    You have a function called Blank, then you do "if(Blank)" which essentially takes teh address of the function and says "if it's not zero, perform the following code". Since Blank is a function, it won't EVER be zero, so you could just as well not have the if-statement.

    Then you do _inp(Blank) - what's this supposed to do, again it uses the address of the function to do something - not sure where 4B comes into it...

    Note that when referring to symbols, the symbol that is closest (in an "going outwards") will be the one used, so the integer variable called Blank further out isn't the one used by the compiler.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    C++ does have a string class.

    http://www.cprogramming.com/tutorial/string.html

  8. #8
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6
    Kernel,

    Thanks for your input. After 6 hours of sleep for the 1st time in 3 days I can now see how the example code could be confusing. I'll try it again.

    Code:
    BSTR __stdcall This()
    {
    if(Something)
      {
      strcat(Bytes, "0");
      }
      itoa(_inp(Blank),NxtBt,10); // decimal value of 4Bh as string "075"
      strcat(Bytes,NxtBt); // array[0]="075"
    Servo

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to initialize Bytes before you strcat to it. Either use strcpy(bytes, "") before the first strcat, or use strcpy(bytes, "0"); else strcpy(bytes, "");

    --
    Mats

  10. #10
    Registered User Servo Wizard's Avatar
    Join Date
    Sep 2007
    Location
    Wichita, KS, America
    Posts
    6
    Kernel,

    Yes you're right, strcpy would have been a better choice for first use of the variable, but it works with strcat. I think that iMalc might have explained why my returned string is compounding but I have yet to figure out how to correct it. I learned VB the same way that I'm attempting to learn C, the internet is worlds best text book. I rarely beg for help but I'm really jammed up tight on this problem.

    Servo

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It works because the variable is initialized to zero before the first use - but if you call the function again, it will add the zero at the end of the previous string - do this a few times and you start overwriting the next string, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Servo Wizard
    I learned VB the same way that I'm attempting to learn C, the internet is worlds best text book.
    Usually yes, but remember not to be unfair but there are a lot of unqualified people who find it "fun" to write tutorials which may not be to the standard of a well written book - by a well regarded publisher, K&R for example.

    But it is true the internet is often the most convenient for reference.

  13. #13
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by xp5 View Post
    I'm reading this programming book and it gives a sample code:
    Code:
    int get_maxpos(int x[], int eff_size)
    {
      int i, maxpos = 0;
    
      for (i = 0; i < eff_size; i++)
        maxpos = x[i] > x[maxpos] ? i: maxpos;
      return maxpos;
    
    }
    Books can also be badly written. Who in their right mind reccomends leaving out braces in a for loop? /me shakes head sadly

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #14
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by QuantumPete View Post
    Books can also be badly written. Who in their right mind reccomends leaving out braces in a for loop? /me shakes head sadly

    QuantumPete
    There's nothing wrong with that...

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by zacs7 View Post
    There's nothing wrong with that...
    Just because the language supports it, doesn't mean it should be done!

    Code:
    if (false == result) 
        action_on_false (arg1, arg2);
    Then you want to print an error message as well:
    Code:
    if (false == result)
       printf ("result is false\n");
       action_on_false (arg1, arg2);
    Now action_on_false will always be executed and it's a hard bug to spot if result *usually* is false (such as when you're checking the return code of fopen). Defensive programming saves hours of debugging!
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  2. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  3. Good news, and bad news... (meaningless post, do not wast your time reading it)
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-05-2002, 03:27 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21