Solved

Migrating from VB to C and C++

Posted on 2003-10-28
6
252 Views
Last Modified: 2010-04-15
Hi C people,

I've been given the daunting challenge of rewriting 10,000 lines of VB code into C within a few days.

Now I was a C expert in 1993, but none since. So I dug out an old program and relearned the basics in a couple of hours - no problem so far.

But I want this code to be as slick and neat as possible, it must work on every version of windows and not require any DLLs except kernel32, user etc. My program will not require a user interface, just collect system info and write a file.

So my questions are:

- Can I use C++ object oriented language without needing DLLs. E.g. can I use the "String" class to manage strings ?
- Whats the best and fastest way to write a file nowadays - fprintf ?
- Anyone got any good examples of how to do "createobject" in C, in a way which doesn't crash if the object isn't installed ?
- Do we still have to use malloc and free etc or is there a better way ?
- If I can use classes, what are the most common classes
- Any standard libraries out there for Split, Join, Replace, Array handling, String handling, etc so I don't have to write them from scratch ?
- General guidelines on good practice would be appreciated

The resulting code must work on a clean build of win 95, win 98, NT4 sp1 etc etc, so no oleaut32.dll, mfc32.dll etc allowed

Don't want any essays, just a few "pointers" ha

Thanks
0
Comment
Question by:xassets
  • 3
  • 3
6 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

Wow!  Have you ever got your work cut out for you!

Here's an answer to most of your questions:

- Can I use C++ object oriented language without needing DLLs. E.g. can I use the "String" class to manage strings ?

Yes.  C++ is really just a wrapper around C.  C++ programs can be just as "stand alone" as are C programs.


- Whats the best and fastest way to write a file nowadays - fprintf ?

It still works just fine.  In C++, reading/writing stdout and stdin has a different look, but everything that you're used to in C still works, too.


- Anyone got any good examples of how to do "createobject" in C, in a way which doesn't crash if the object isn't installed ?

CreateObject means different things to different people.  And is a lot different when C++ is involved.  What's your meaning here?


- Do we still have to use malloc and free etc or is there a better way ?

C still uses them.  C++ will also let you use them, but C++ does have a better way.  Class instantiation in C++ (creating of a typedef'd object) automatically assigns memory and runs initialization code.  It's quite a nice extension to C.


- If I can use classes, what are the most common classes

You can't use classes in C, but you can in C++ (of course).  Since classes are really just struct-like items, you'll uses whatever classes you need.


- Any standard libraries out there for Split, Join, Replace, Array handling, String handling, etc so I don't have to write them from scratch ?

Ansi Strings are worth their weight in gold.  (How much does a bit weigh?  :) )  The automatically assign memory, reallocate to the correct length as needed, and have a lot of built in APIs to do the kinds of things that you normally would do to strings.  strlen(), strcpy(), split, etc.


- General guidelines on good practice would be appreciated

Pretty broad question here.  What are you looking for?


Kent


0
 
LVL 4

Author Comment

by:xassets
Comment Utility
Thanks Kent, pretty quick response. The C++ route sounds a lot cleaner given the timescales involved

CreateObject is for creating wmi objects, but obviously some systems won't have it installed, so I want to check if its installed/creatable/gettable and then do the C++ equivalent of getobject("winmgmts!...". Just need an example

General guidelines and most common classes - just some pointers to examples would be useful - so I can code along the lines of what is generally accepted good practice and so that the next C programmer to pick up my work doesn't think I'm a dork (well at least minimise that perception).

Cheers



0
 
LVL 45

Accepted Solution

by:
Kdo earned 150 total points
Comment Utility

Here's a quick primer on C++.

C++ has added a LOT to C so that a program deals less with logic flow and more with data management.  In C, to "manage" something like a small address book, you'd define a data structure (using struct) and then write code to update and query each record.  In C++ you could do the same, though to do it "natively" you'd probably create a class instead of a struct.  Here's a comparison.

typedef struct
{
  char Name[20];
  char PhoneNumber[20];
  char Street1[24];
  char Street2[24];
  char City[18];
  char State[2];
  char Zip[10];
}  AddressEntry_t;

AddressEntry_t **AddressEntry = NULL;  /*  Array of pointers to AddressEntry records  */
AddressEntry_t *CurrentAddress;

AddressEntry_t *CreateNewAddressEntry ()
{
  /*  Create a new Address Entry and add it to the table  */
}

AddressEntry_t *FindAddressEntry (char *Name)
{
  /*  Find an address entry in the table and return a pointer to it  */
}


Rewriting this shell in C++, you could have something like this:


class TAddressEntry
{
  public:
    TAddressEntry();
    ~TAddressEntry();

    char Name[20];
    char PhoneNumber[20];
    char Street1[24];
    char Street2[24];
    char City[18];
    char State[2];
    char Zip[10];

    void SetName (char *Name);

};

class TPhoneBook
{
  public:
    TPhoneBook ();
    ~TPhoneBook ();

  TAddressEntry **AddressList;
  TAddressEntry *CreateEntry (void);
  TAddressEntry *FindAddressEntry (char *Name);
};

TPhoneBook::TPhoneBook ()
{
  AddressList = NULL;
}

TAddressEntry *TPhoneBook::CreateEntry (void)
{
  TAddressEntry *NewEntry;

  NewEntry = new TAddresEntry;

/*  Put item on list  */
}

TAddressEntry *TPhoneBook:: FindAddressEntry (char *Name)
{
  /*  find and return the pointer to the correct item  */
}



main ()
{
  TAddressEntry *CurrentEntry;
  TPhoneBook *PhoneBook = new TPhoneBook ();
  CurrentEntry = PhoneBook->CreateEntry ();
  CurrentEntry->SetName ("Kent");  /*  Not shown  */
...
}


C++ is a lot wordier, but it can remove a lot of the headaches associated with pointer management!

Kent
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Author Comment

by:xassets
Comment Utility
OK thats really useful, even though I never did C++ in anger it all makes sense.

If you can also find a simple example of using create object and then calling methods (say Scripting.FileSystemObject as an example) that would be really useful, but I guess I could find that on msdn anyway.

Thanks for all your help, and have some points

0
 
LVL 4

Author Comment

by:xassets
Comment Utility
KDo,
Just started this, and can't seem to get any kind of string class working. It seems to work OK in managed .net apps, using CString class, but that seems to require a DLL. I want to be able to use a c++ string class without carrying dlls is this possible ? no probs if you want points
thanks
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility

What you're seeing is that different vendors have different implementations of string classes.  Borland's Delphi uses an entity called an AnsiString.  Their C++ uses a class called String, but also recognizes their AnsiString.

I'm not sure what the class(es) are called in Micro$oft.


Kent
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

11 Experts available now in Live!

Get 1:1 Help Now