Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Migrating from VB to C and C++

Posted on 2003-10-28
6
Medium Priority
?
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 9636005

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
ID: 9636148
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 46

Accepted Solution

by:
Kent Olsen earned 600 total points
ID: 9636229

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:xassets
ID: 9636283
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
ID: 9827515
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 46

Expert Comment

by:Kent Olsen
ID: 9827644

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

636 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