How can one gain weight, at the same time not becoming overweight?
It is commonly known that muscle weighs more than fat. I agree with the the precautions mentioned in the first post, but I will add that I think one of the most common ways to gain weight but not necessarily risk becoming overweight, is to increase muscle mass.
Many people starting new exercise and weight-training routines to lose weight complain of losing very little or even gaining at the beginning. The initial conversion of fat to muscle often results in weight maintenance rather than an initial loss (especially if there is no change in eating habits). If a person is already quite thin, exercises to add muscle mass will require additional calories for fuel. Most experts recommend eating more protein, carbohydrates, and "healthy" fat in order to provide the body with the energy it will be burning.
Finally, in the same way that losing weight is a process that should not be rushed, gaining weight also takes time. The ability to control how much weight is gained (without going overboard) can be largely maintained by making it a slow and steady process. Most experts would agree that any major physical change requires patience and persistence.
I do not think this question is one that can be answered here, especially as you have referred to the fact that you are looking for medicines to increase your weight.
You should contact a certified medical professional. There are many reasons why people do not put on weight; they could be linked to a hormonal imbalance, deficiency of some nutrients, a wrong diet, etc.
A medical professional will be able to first determine if everything is fine with you and then refer you to a nutritionist who will be able to analyze your diet and your daily activity to advise the changes that you need to make.
You would most probably be asked to eat a lot of healthy foods, increase the intake of calories, maybe take nutritional supplements, and the alike.
Do not ask for advice from your friends and others without an adequate knowledge on this subject. Taking the wrong pills to achieve what you want to do can be injurious to you.