Page 1 of 1
c++ questions
Posted: March 2nd, 2006, 1:45 pm
by NewGuy
To follow up on my previous post I have some additional general c++ questions:my previous post1) Can someone provide me with some guidance on string usage in c++. In the case of my previous post I know that output to VBA must be a BSTR (or should it be a CComBSTR) so I am using those for output. In the guts of my code, being that I come from a java/c# background, I use std::strings because of their ease of use. Finally the INTEX api that I am coding against requires char* for all string IO. So I'm stuck converting from BSTRs to strings to char* and it's a bit messy... there must be a better way. Any guidance on this is much appreciated.2) When returning results from a function call is it better to pass the output as a param by reference or to return output from the function. Passing output by reference seems preferable to me at this point as I can return success/fail info as the return value and data output as a parameter. I can also return multiple pieces of data this way. Is there a right or preferred method to determining which output technique to use?3) Can someone provide me with a resource (website, book, class, etc..) to give me some exposure to writing .dlls in c++. I basically want to write a library of functions for my company for commonly used INTEX calculations to be used in other applications, both c++ and vba. I think I could do this now, but if it will be done the "correct" way I doubt as I am basically stumbling my way around this process. I want to make sure I do it right the first time to both benefit my company and my career prospects
c++ questions
Posted: March 2nd, 2006, 3:13 pm
by fradiavalo
Looking at your previous post, I strongly advise you to learn some basic c++ first.You are using 'new' to allocate memory and then using 'free' to deallocate. Thats a sin. Never mix those. use 'free' with malloc and delete with new. Please learn some basic pointer stuff in C++ and you should be fine.Regarding functions, it all depeneds what you wanna do with them really......if you want to daisy chain, you should return objects so that you can do something like foo().goo() ... so it really all depends on the design on your code and consistency with other methods. There isn't a recommended way per say. For C++, I recommend stroustrup's book as well as effective C++ series to get started.
c++ questions
Posted: March 2nd, 2006, 3:27 pm
by rtougher
QuoteOriginally posted by: NewGuy1) Can someone provide me with some guidance on string usage in c++. In the case of my previous post I know that output to VBA must be a BSTR (or should it be a CComBSTR) so I am using those for output. In the guts of my code, being that I come from a java/c# background, I use std::strings because of their ease of use. Finally the INTEX api that I am coding against requires char* for all string IO. So I'm stuck converting from BSTRs to strings to char* and it's a bit messy... there must be a better way. Any guidance on this is much appreciated.2) When returning results from a function call is it better to pass the output as a param by reference or to return output from the function. Passing output by reference seems preferable to me at this point as I can return success/fail info as the return value and data output as a parameter. I can also return multiple pieces of data this way. Is there a right or preferred method to determining which output technique to use?3) Can someone provide me with a resource (website, book, class, etc..) to give me some exposure to writing .dlls in c++. I basically want to write a library of functions for my company for commonly used INTEX calculations to be used in other applications, both c++ and vba. I think I could do this now, but if it will be done the "correct" way I doubt as I am basically stumbling my way around this process. I want to make sure I do it right the first time to both benefit my company and my career prospects Regarding #1, the "_bstr_t" class might help you out. It encapsulates the BSTR type, it contains the user-friendly operator overloads that you are are used to (+, etc), and there is an overload for (char*). Maybe give this a shot. In general, dealing with strings in COM is a headache.Regarding #2, I prefer to use the return value to return the results of the function, and then throw exceptions anytime something goes wrong.Regarding #3, you might want to write a COM dll, which would be callable from any COM-aware language (VB, C++, C#, etc). For authoring C++ COM dlls I would look into the Active Template Library -- I'm not sure if Microsoft still supports this, but I remember it being very straightforward. Microsoft has information about ATL at MSDN. If you are using managed C++ you could write a .NET dll and then expose it as a COM dll (interop?).- Rob
c++ questions
Posted: March 2nd, 2006, 3:39 pm
by NewGuy
rtougher,Thanks pointing out my error. I made the fix to my code. I realize I need to brush up on my c++ skills, but unfortunately I have no time to do that at work and I'm currently studying for the CFA in the evenings. I do have the "thinking in c++" series by Eckel in html format that I plan on reading when I have time.I do wish I had time to brush up on my c++ before jumping into these projects, but I don't so I need to at least put something together for now and I can't for the life of me get the string passing from c++ to vba to work.Thanks for your advice though.
c++ questions
Posted: March 2nd, 2006, 3:42 pm
by NewGuy
Sorry that post was meant for fradiavalo.Rtougher, thanks for your input. I will look into your suggestions when I get a chance... for now I'm still focusing on getting my problem from my previous post working. Do you have any thoughts on why I'm not getting anything results for my BSTR output?Thanks
c++ questions
Posted: March 2nd, 2006, 4:21 pm
by rtougher
QuoteOriginally posted by: NewGuyRtougher, thanks for your input. I will look into your suggestions when I get a chance... for now I'm still focusing on getting my problem from my previous post working. Do you have any thoughts on why I'm not getting anything results for my BSTR output?ThanksWithout knowing what cGetAllBondDependencies is doing, I can't be sure what might be happening. I would suggest that you stay away from new, delete, alloc, and free, though. You can instead declare your strings on the stack (i.d. 'std::string s = "Hello World"'), so that you leave the memory management up to the string class.To be honest, you might want to re-evaluate your choice of C++. Dealing with the interaction between C++ and VB is difficult for people that know C++ well -- I can't imagine how it might be for a C++ beginner. Not that I'm a superstar or anything, but I spent a few years writing ActiveX controls in C++ (for use in VB), and I shudder when I think back to all of the hoops I had to jump through (BSTRs, a dozen mandatory COM interfaces, etc).- Rob
c++ questions
Posted: March 2nd, 2006, 6:24 pm
by NewGuy
cGetAllBondDependencies is just a call to code I wrote to extract information from a third party (Intex) C api (hence my need to program in c++). The code works in c++. I have the project set up to be a console.exe program in debug and a .dll in release so that I can test my functionality in c++ before trying to call the code from vba. So the problem seems to be just the vba/c++ interface.I've actually made some progress and now have the vba function returning a value although it is inserting an extra byte between each character in the returned string. For example if I was expecting a return value of "COLLAT_1" (and I get that when testing in c++ from main()) then in vba my returned value is being padded by an extra byte (00) for each character. Here is my new code using your and fradiavalo's suggestions:Thanks again for your help.VBA:Public Function getAllBondDependencies_vba(sInpt As String, ourCDI As String, output As String) As Integer Dim result As Integer result = getAllBondDependencies(sInpt, ourCDI, output)End FunctionC++:int __stdcall getAllBondDependencies(const char* const input, const char* const our_cdi_path, BSTR* dependencies){ int ret_val; char* temp_val; string s_dependencies; string s_input(input); string s_our_cdi_path(our_cdi_path); ret_val = cGetAllBondDependencies(&s_input, &s_our_cdi_path, &s_dependencies); temp_val = strdup(s_dependencies.c_str()); *dependencies = A2BSTR(temp_val); delete temp_val; return ret_val;}
c++ questions
Posted: March 2nd, 2006, 11:10 pm
by asimeqi
1. How do you see that the returned string in VBA has those extra bytes and why is that a problem for you?The returned string is Unicode so it has to have the extra bytes. That should not create any problems ifyou use the string in VBA as it is inteded to be used.2. In your C++ code you must check the return value from cGetAllBondDependencies().3. "delete temp_val" is wrong.You must write free(temp_val) since temp_val is created with malloc().In general there is little need to use C functions such as strdup() in C++ code.Moreover in your case you do not need temp_val at all. Just say: *dependencies = A2BSTR(s_dependencies.c_str());
c++ questions
Posted: March 3rd, 2006, 2:51 pm
by NewGuy
1. If i hover my mouse over the variable or put it in the watch window in the excel vba editor then then unexpected character is displayed as a square and is inbetween each expected character. The unexpected character is 00 the null character so when I attempt to display the character or insert it into a cell on my spreadsheet only the first letter, a C in this case, is displayed. I realize the returned string is Unicode and is expected to have extra bytes, but vba should handle those extra bytes because under the hood vba uses Unicode strings as well correct? On the vba side of things I'm DIMing a new string and passing it to my c++ function using ByRef. That's the correct usage right?2. I'm not sure why you say I need to do this.3. Thank you... it's things like this that make me realize I need to find some time to read a good c++ book. There are so many ways to do something in c/c++ and whether it's the correct way to do something or not is something I only recently started paying attention to.Thanks for the help.
c++ questions
Posted: March 4th, 2006, 6:04 pm
by asimeqi
1. I was not aware but it seems that Visual Basic will try to change your string to Unicode eventhough it is already Unicode. That explains the strange characters you are seeing.It is easy to fix that. In your Visual Basic code say:Dim goodOutput As StringgoodOutput = StrConv(output, vbFromUnicode)and then use goodOutput2. All I was trying to say was that the return values form cGetAllBondDependencies() must have some meaning.Say for example ret_val = -1 means the function failed.In that case you should not continue with your function but return with an error code of your own.
c++ questions
Posted: March 6th, 2006, 12:46 pm
by NewGuy
That worked. Thank you so much.
c++ questions
Posted: April 12th, 2006, 1:25 pm
by lisiandrea
hi!i have a problem with the UCSD_toolbox; when i try to use the m file "multigarch.m" matlab tells me that the function "tarchcore" is undefined! I guess the problem is that this function has been created with C++, it seem my matlab can't import this function created in C++.Any suggestions