|
how would i go about converting this following code to c# my friend sent it to me but its in c++ and i have no idea what to do with it as i code in c# code
namespace Goat
{
struct IBaseWeapon
{
virtual ~IBaseWeapon() {}
virtual string GetName(void) = 0;
virtual bool AddToCsv(string& strOut) = 0;
};
class Kap40 : public IBaseWeapon
{
public:
Kap40() = default;
virtual string GetName(void)
{
return "kap40";
}
virtual bool AddToCsv(string& strOut)
{
strOut += "xmodel, kap40_viewmodel\n";
strOut += "xmodel, kap40_worldmodel\n";
return true;
}
};
class Ak47 : public IBaseWeapon
{
public:
Ak47() = default;
virtual string GetName(void)
{
return "ak47";
}
virtual bool AddToCsv(string& strOut)
{
strOut += "xmodel, ak47_viewmodel\n";
strOut += "xmodel, ak47_worldmodel\n";
return true;
}
};
class WeaponsMng
{
public:
WeaponsMng() = default;
void AddWeapon(IBaseWeapon* pWeapon)
{
weapons_.push_back(pWeapon);
}
void RemoveWeapon(const string& name)
{
for(auto w : weapons_)
{
const string& curName = w->GetName();
if(curName == name) {
weapons_.remove(w);
}
}
}
string CreateCsc(void)
{
string res;
for(auto w : weapons_)
{
w->AddToCsv(res);
}
return res;
}
private:
std::vector<IBaseWeapon*> weapons_;
};
WeaponsMng mng;
void OnAddKap40_checkbox(bool isChecked)
{
if(isChecked)
mng.AddWeapon(new Kap40());
else
mng.RemoveWeapon("kap40");
}
void OnAddAk47_checkbox(bool isChecked)
{
if(isChecked)
mng.AddWeapon(new ak47());
else
mng.RemoveWeapon("ak47");
}
}
thank you in advance
elfenliedtopfan5.
|
|
|
|
|
Understand exactly what this code is doing and rewrite in C#. You're friend sent it to you so he must have written it correct? He can explain it to you too. Nobody is going to convert it for you.
It would seem there is a ton more going on in the rest of the code that this code interacts with, so a line-by-line conversion is probably not going to do anything for you.
|
|
|
|
|
You can try using the free C++ to C# converter here: [^], but I think the "translation" will only be useful to give you ideas about what the C++ code is doing, and how specific syntaxes map from C++ to C#. And, the output is likely to need considerable "massage."
Until you understand the purpose and usage of the code, you are dancing in the dark.
What I can say about the structure of the code, speaking as a "C/C++ non-native speaker", from a quick glance: ... of course you'll have a NameSpace ....
a. you have an Interface 'IBaseWeapon ... the on-line converter would convert that to an abstract class.
b. you will have (not static) classes, WeaponsMng, Ak47, Kap40
b.1. Ak47 and Kap40 will inherit from 'IBaseWeapon
b.2. WeaponsMng keeps a List of IBaseWeapon
c. you will have some static methods which should be put in a static class
«In art as in science there is no delight without the detail ... Let me repeat that unless these are thoroughly understood and remembered, all “general ideas” (so easily acquired, so profitably resold) must necessarily remain but worn passports allowing their bearers short cuts from one area of ignorance to another.» Vladimir Nabokov, commentary on translation of “Eugene Onegin.”
|
|
|
|
|
I don't think it is productive to use a converter in this case because
C# classes are managed classes while the given C++ class is a native one.
For a Managed C++ (or formally, C++/CLI) class, we should write ref class instead of class - and objects are instantiated in this way:
Object^ obj1 = gcnew Object(); Object^ obj2 = ref new Object();
|
|
|
|
|
Did your friend tell you what the code is supposed to do?
I can tell you it doesn't really "do" much of anything; the "intent" seems to be to provide functionality that will add / remove a weapon from your inventory; but by itself, the code cannot even do that unless it is properly integrated with the "main application" (whatever that may be).
|
|
|
|
|
Hi All,
I am trying to start remote machine process using C# WMI using below code
Objconnection.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", strRemote), Objconnection);
manScope.Connect();
Objconnection.EnablePrivileges = true;
objSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Process");
opsObserver = new ManagementOperationObserver();
objSearcher.Scope = manScope;
ManagementPath managementPath = new ManagementPath("Win32_Process");
ManagementClass processClass = new ManagementClass(manScope, managementPath, new ObjectGetOptions());
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
inParams["CommandLine"] = "fismqsrvC.exe";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
uint rtn = System.Convert.ToUInt32(outParams["returnValue"]);
uint processID = System.Convert.ToUInt32(outParams["processId"]);
Console.WriteLine(rtn);
Console.WriteLine(processID);
Above code working correctly for independent processes. But not able to start Process which is Coming Under Particular service. any direction and help appreciated.
Tejendra
|
|
|
|
|
Is this a Windows Service app or a standalone application you're trying to start?
If it's a standalone application, you may be able to start it, but there are some severe restrictions. It can NOT be a user interactive application. This means there can be no user interface if it's a Windows application and for a Console application, it can not wait for user input at all. The user will never see the interface and will never be able to interact with it.
For a Windows Service application, you cannot start it using the Win32_Process WMI class. You have to start it using the Win32_Service class[^].
modified 6-Feb-16 15:28pm.
|
|
|
|
|
Thanks for direction Dave.
I am writing console application which I am planning to schedule on remote server.
I am able to start independent process using my posted code.
But I am not able to start the Process which is coming under Particular service(Means any process which is tightly coupled with Service).
The option I can see here to restart service which will restart related processes. The disadvantage is this activity it will restart my all processes which i don't want to.
let me know your thoughts.
Thanks
|
|
|
|
|
You simply don't have a choice here. Either your app works if started by your Win32_Process code or it doesn't and you have to stop and restart the service.
|
|
|
|
|
yup, Thanks for the Help Dave.
|
|
|
|
|
I have a wrapper class around serial port which looks something like this:
static class HASPCLass
{
private static SerialPort m_port;
private static bool m_initialized;
private static int m_baudRate;
static readonly object _syncObject = new object();
public DoInitialization(int baudRate )
{
lock(_syncObject)
{
if (!m_initialized)
{
Initialize(baudRate);
}
}
}
private Initialize(int baudrate )
{
m_port.open(..);
m_baudRate = baudRate;
m_initialized = true;
}
private Uninitialize()
{
m_port.close();
m_initialized = false;
}
public void Read(byte[] buff)
{
lock(_syncObject)
{
m_port.Read(buff);
}
}
public void Write(byte [] buff)
{
lock(_syncObject)
{
m_port.Write(buff);
}
}
public void Close()
{
lock(_syncObject)
{
if (m_initialized)
{
Uninitialize();
}
}
}
}
I tried making this class thread safe. Someone initializes it - read and writes maybe used from other threads - and in the end calls Close.
Now Imagine I have two additional static methods from other class which do something like this:
public static void function1()
{
HASPClass.Read(...);
HASPClass.Write(...);
}
public static void function2()
{
HASPClass.Read(...);
HASPClass.Write(...);
}
For overall thread safety I also enclosed these functions in locks:
public static void function1()
{
lock(otherlock1)
{
HASPClass.Read(...);
HASPClass.Write(...);
}
}
public static void function2()
{
lock(otherlock1)
{
HASPClass.Read(...);
HASPClass.Write(...);
}
}
Because order in which read and writes are called might be relavant for the serial port/HASP.
My question is: is now my final approach (of using function1 and function2) correct/thread safe? Can I encounter some problems related to threading using function1 and function2?
modified 5-Feb-16 8:08am.
|
|
|
|
|
Locking everything is one way.
Static members are thread-safe as long as they don't access shared resources. The thread creating the serial-class would probably also be the one consuming it; only one consumer per port anyway.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Could you please elaborate your answer, I didn't get you
|
|
|
|
|
Start here[^], and remember that a serial port is not meant to be shared among thread or applications.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Can you please tell me now how all this relates to my question? I have seen posts like the one you linked
|
|
|
|
|
I don't see the need for locking on a static method unless it accesses class-information or a shared unmanaged resource.
Also, adding "lock" everywhere feels like an on error resume tactic
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
you mean locks in HASP class or locks in function1/2?
in function1/2 I added because it can be function1 called read, then function2 called read and write - now function1 calls write - will the output be what I expected? depends on the HASP device.
|
|
|
|
|
Member 12061600 wrote: will the output be what I expected? Yes, as the read would have to finish before something else can be done with the port.
Which HASP dongle are you using? Don't they go by the name "sentinel" these days?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
What kind of serial device requires a read, THEN a write?
|
|
|
|
|
Maybe he's writing code for the server side?
|
|
|
|
|
But what he's got looks like "slave" code; usually in firmware.
The master does the broadcasting (i.e. write) and then expects / awaits an answer (read).
|
|
|
|
|
Right; that's what I meant by "server".
|
|
|
|
|
Fair enough ... just never used serial com in a "client-server" setup; only master-slave.
For example, a printer only prints when you tell it to; it doesn't ask for something to print (usually).
A scale tells you the weight only when you ask.
etc.
|
|
|
|
|
Potato / potatoe.
One of the first serial devices I wrote software to communicate with was actually a fairly large specialized computer that performed analyses of gasses.
But most serial devices I encountered were smaller, e.g. a coin counting machine, a magnetic card encoder. Good times.
|
|
|
|
|
But I still don't know if OP is writing client code or server code ...
|
|
|
|