Implement C++ method in source file without updating header file using EnvDte - c++

I am writing a visual studio extension using Visual Studio SDK. I have a C++ header file (Foo.h) that contains declaration of a class with a static method.
Class Foo {
static void Func(int param);
};
I want to add a new source file (Foo.cpp) and implement the method of the class in it from scratch using EnvDte without updating existing header file.
I have tried several methods but each one has different problems.
VCCodeClass::AddFunction() method with location of source file with the following arguments:
// "VCCodeClass codeClass" is variable for class at header file
// "string sourcePath" is variable for source file(Foo.cpp) path
codeClass.AddFunction("Func", vsCMFunction.vcCMFunctionShared, "void", -1, vsCMAccess.vsCMAccessPublic, sourcePath);
It adds implementation to Foo.cpp but also adds same declaration to header file.
VCFileCodeModel::AddFunction() method with the following arguments:
// "VCFileCodeModel vcFileCodeModel" is variable for FileCodeModel of ProjectItem of Foo.cpp file
CodeFunction func = vcFileCodeModel.AddFunction("Foo::Func", vsCMFunction.vcCMFunctionShared, "void", -1, vsCMAccess.vsCMAccessPublic);
It adds the method to source file correctly without parameter (static void Foo::Func(){}). But then when I try to add parameter with VCCodeFunction::AddFunction() method with the following arguments:
func.AddParameter("param", "int", -1);
It throws error: "Value does not fall within expected range".

Related

How to format C++ code in Visual Studio using my own rules?

I am using Visual Studio 2019.
I want to create a setting file for Visual Studio which consists of my own formatting rules and anyone can import those settings for their Visual Studio. (I think this is a simpler way for a newbie instead of instructing him to install a format code extension). I name this file MyFormattingRules.vssettings. Am I right about this file extension?
How to set the formatting rules in this file using my own rules?
I found this link but it does not really do what I want.
https://learn.microsoft.com/vi-vn/visualstudio/ide/cpp-editorconfig-properties?view=vs-2019
For example:
If a function has only 1 input parameter, then we write it in only one line, but if it has from 2 input parameters, then we write each parameter in one line
Example1.h
void myFunc(int param);
void myFunc2(
int param1,
int param2,
int param3);
When I declare a variable in a class, after saving my file, it will change the name of my variable.
Example2.h
class Myclass
{
public:
MyClass();
private:
int myVar;
}
After saving, myVar should become m_myVar. Is this possible to do such things with the setting file? If not, can you suggest a tool that can do this?
After importing my setting file, how to do such that whenever I save my file (.cpp or .h), my formatting rules will be applied?
Can you give me an example of such a file?

How to know which method is not implemented for C2259 VS 2017 error?

I am defining a new interface and a class that implements it. I have added my class(XXX) and interface(IXXX) in idl file (I generated two uuids). My interface has two simple methods. I have implemented them( files XXX.h and XXX.cpp). I included the following line in InprocServer.cpp.
UNKNOWN_OBJECT(XXX)
I am getting the following error:
Error C2259 'XXX': cannot instantiate abstract class (compiling source file InprocServer.cpp)
clicking on error takes me to the following line in
Unknown.h
try
{
**p = new T(pUnkOuter); // refcount is already 1**
if (!p)
return E_OUTOFMEMORY;
}
How do I know which method is not implemented?
Look in the output window (View->Output)rather than the error list and any unimplemented members will be part of the diagnostic.
This works so long as you have the setting in Tools->Options, Projects and Solutions->Build and Run for MSBuild verbosity set to at least 'Minimal', I'm not sure about 'Quiet'.

How to include a .h or .dll file in CANoe/CAPL

I want integrate a header .h or .dll file in CAPL (concretly Visa32.dll, visa.h or sicl.h) to control a multimeter 34461A. How can I include the .h files or .dll file in CANoe?
I created an ECU module called multimeter. Thanks,
Including external DLLs in CAPL is possible, but you will need to create a wrapper for all the functions you're going to use.
Take a look at \CANoe\Demo_AddOn\Capldll directorty which features such a wrapper. It's a MSVC project exporting a few simple functions to CAPL, like int f(int a, int b) {return a+b;}.
What you will need to to is to add your library files (Visa32.dll, visa.h) to this Capldll project and define wrappers for all the functions you want to call from CANoe. For example, if you have int visa_init(double arg) in Visa32.dll, you will create a wrapper:
int CAPLEXPORT far CAPLPASCAL capl_visa_init(double arg)
{
return visa_init(arg);
}
You will also need to add the prototype of your function to the export table:
CAPL_DLL_INFO CAPL_DLL_INFO_LIST[] =
{
{"my_visa_init", (CAPL_FARCALL)capl_visa_init, 'D', 1, "F", "\000"},
....
{0,0}
};
Once you have successfully build your wrapper DLL (it will be called capldll.dll if you reuse the example), you will need to import it in CANoe, and you will be able to call the function by the name you defined in the export table, e.g. my_visa_init(1.0);
CAPL is not C. You can not include .h files.
The easiest would be to control the multimeter over the GPIB bus. Take a look at the CAPL GPIB library.

Hiding use of class inside a library

I am creating a simple library for Arduino, the intent of which is to wrap and hide another class inside of it. I would like to be able to hide the underlying class entirely from the end user.
I have somewhat simplified this example to keep things clear, but the same problem exists.
The class I am trying to wrap is the Wire library which is called TwoWire.
The header file of the wrapper library:
#ifndef __DERIVEDONEWIRE2_H__
#define __DERIVEDONEWIRE2_H__
#include <Wire.h>
class DerivedWire
{
private:
TwoWire wire;
public:
DerivedWire();
};
#endif
The CPP file:
#include "DerivedWire.h"
DerivedWire::DerivedWire()
{
}
And the Arduino sketch that uses it:
#include <DerivedWire.h>
DerivedWire derivedWire;
void setup()
{
}
void loop()
{
}
This fails to compile:
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: 'TwoWire' does not name a type
Including Wire.h at the top of the main sketch allows this to compile:
#include <DerivedWire.h>
#include <Wire.h>
DerivedWire derivedWire;
void setup()
{
}
void loop()
{
}
However I would like to completely hide from the user of the DerivedWire library that the Wire library/TwoWire class is used at all.
I have tried changing the line "TwoWire wire;" to "TwoWire* wire;" - now I get the following error:
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: ISO C++ forbids declaration of 'TwoWire' with no type
/Users/andrew/Documents/Arduino/libraries/DerivedWire/DerivedWire.h:9:
error: expected ';' before '*' token
I've tried a few other things, but it just seems that I need to include Wire.h at the top level.
How do I fix this?
The Wire instance is created on the last line in the lib.
https://github.com/lstoll/arduino-libraries/blob/master/Wire/Wire.cpp
You are NOT supposed to have multiple instances of TwoWire, and there is no reason to hide it, but if you really want to hide it, you have to remove that line.
The arduino build method requires that the lib is included from the main sketch so you can not hide that part without signifigant changes.
If you really really need to hide this Wire instance, you could copy the source of the Wire lib into your own lib, and remove that last line.
Check out the "pimpl idiom", here's a link to get you started: http://en.wikipedia.org/wiki/Opaque_pointer
Pimpl idiom is a way in C++ to hide a class implementation, and all functions that you do not want to be visible.

C/C++ namespace in header issue

I am trying to implement a Websocket Server. I am using the libwebsockets library.
ConnectionServer.c file has setup code for the library and main() function (I don't see anything of importance to post here.) This file includes 1 file for the received data callback called:
dmserver_callback.cpp.
This file then includes another file (my custom parser file) called:
data_parser.cpp.
This file then includes a file called definitions.h (the source of the problem).
Just bear with me; I understand that including files (daisy chaining; so to speak) probably isn't the best way to do this, and I more than likely should be using header files and such. One question I have is that is this particularly necessary?
To clarify, everything is working as intended until I try to add my own parsing mechanism.
The definitions.h file is as follows:
namespace EngineDefinitions {
enum Version {
Major = 1,
Minor = 2
}; //Version;
namespace Server {
enum enum_Server {
MAX_PLAYERS = 200,
MAX_TABLES = 42, // 3 tables per row.
MAX_TABLE_PLAYERS = 10,
GAME_PORT = 2040, //2042
MAX_PARAMS = 10
}; //Server;
};
namespace Login {
enum enum_Login {
USERNAME = 1,
PASSWORD = 2
}; //Login;
};
};
My error is:
definitions.h(1): error C2061: syntax error : identifier 'EngineDefinitions'
I loaded the exact same header in a new Win32 Console Project in Visual C++ 2010 Express and there it works. The only difference I see is the main file (where int main function resides).
In the project that the header file works is called:
ConectionServer.cpp (C++)
and the main project file that doesn't work is named:
ConnectionServer.c (C)
Does this have something to do with the file being compiled in C vs C++?
I think that the libwebsocket library is coded in C.
I can't recall if I created the project files in exactly the same manner or not.
P.S. Please let me know if there is any other information I can provide to help.
EDIT: I also realize you're not supposed to define types inside a header file (eg: enums).
I DID try to separate the source into .cpp and a header file using the extern enum
with no difference. In fact, got more errors (redefinitions) than I bargained for when trying to use them.
You need to understand that C++ and C are different languages. When you include the header file in a .cpp file, the compiler will try to compile the .h as C++ code. C++ supports namespaces, so everyone is happy. But when you try to include the .h from the .c file (which is what is actually happening if you follow the #includes), the compiler attempts to compile the .h as C code, yet fails because namespaces do not exist in C.
A common way to solve this problem is to use predefined macros. __cplusplus is defined when compiling as C++ code, and not defined when compiling as C code (obviously).
You could do this:
#ifdef __cplusplus
namespace EngineDefinitions {
#endif
enum Version {
Major = 1,
Minor = 2
}; //Version;
#ifdef __cplusplus
namespace Server {
#endif
enum enum_Server {
MAX_PLAYERS = 200,
MAX_TABLES = 42, // 3 tables per row.
MAX_TABLE_PLAYERS = 10,
GAME_PORT = 2040, //2042
MAX_PARAMS = 10
}; //Server;
#ifdef __cplusplus
};
#endif
#ifdef __cplusplus
namespace Login {
#endif
enum enum_Login {
USERNAME = 1,
PASSWORD = 2
}; //Login;
#ifdef __cplusplus
};
#endif
And of course, you lose the ability of namespaces in C anyways.
C does not have namespaces. For Real.
You cannot include a C++ file in a C file (unless it has been prepared for such use). If you then try to compile the C file, it'll try to compile even the C++ file as C. Instead, use separate compilation and header files.
Note that C does not understand namespaces.