• C

Migrating from VB to C and C++

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
LVL 4
xassetsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

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
xassetsAuthor Commented:
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
Kent OlsenData Warehouse Architect / DBACommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

xassetsAuthor Commented:
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
xassetsAuthor Commented:
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
Kent OlsenData Warehouse Architect / DBACommented:

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.