So I have a function, written in C++, that looks like this...
extern "C" __declspec(dllexport) int __stdcall SomeFunction(char *theData)
{
// stuff
}
... and I'm using it in my current project (written in C#). There are other projects that use this function written in VB, looking like this:
Public Declare Function SomeFunction Lib "MyDLL.dll" _
Alias "_SomeFunction@4" (ByVal theData As String) As Integer
So I tried writing an equivalent in C#, but found that using the string type didn't actually work for me - the string would come back with the same data I passed it in with. I tried using "ref string"
instead to pass the string by reference and I got a memory access violation.
After doing some digging, I found that this was the correct implementation in C#:
[DllImport("MyDLL.dll", EntryPoint = "_SomeFunction@4")]
public static extern int SomeFunction(StringBuilder theData);
Now I know that VB.NET and C# are quite different, but I suppose I always assumed that strings were strings. If one language can marshal char*
to String
implicitly, why can't the other, requiring a different class altogether?
(edited the title for clarity)
Why Learn C? There are an awful lot of programming languages available right now -- everything from the extremely high level (such as Visual Basic) to the low level power of assembly, and a good variety of specialized options in between ( Perl, Ruby, and Python are good choices for many tasks).
So # is used by C compiler to know which lines need preprocessing. Why do we use #include in C? In simple words, C has many libraries which consist of a finite number of pre-defined functions. So to use them in our program, we need to import them which we do by using #include <….. .h>.
So why do we need a C? When we combine the C with an H we DO make a unique sound. Without a C we would go to Hurch instead of Church, we would listen to a Hime instead of a Chime, etc. So the C is indeed a very important letter and has no reason to feel ashamed because it makes no sound on it own.
Plus, with C, you get lots of strong opinions mixed with insights that you can understand. As a result of its age and its use as the language of system programming for Unix, C has become something of the lingua franca of programming. C is a great language for expressing common ideas in programming in a way that most people are comfortable with.
Now I know that VB.NET and C# are quite different, but I suppose I always assumed that strings were strings
Strings are immutable in .net. Ask yourself why it is that ByVal
passing of an immutable data type can result in the value changing. That doesn't happen for normal functions, just for Declare
.
I'd guess it all has to do with maintaining some backwards compatibility with Declare
statements from classic VB6 which were done this way. To my mind the black sheep here is the VB.net code rather than the C# code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With