One of the things that C++ doesn’t have out-of-the-box is events, which is not necessarily a bad thing. However, with the additions to the C++0x/C++11 specification, we can implement something like an event system found in higher level languages such as C# using relatively easy to use code.
If you’re having trouble with getting jQuery to talk to your ASP.NET web service, I may have some tips to ease your pain.
#1: It’s not JSON!
That’s right, even though your WebMethod consumes and returns JSON, you actually have to send a formatted string instead of a pure JSON object:
JavaScript:
var BadArguments = { FirstName: "John", LastName: "Doe" }; // BAD
var GoodArguments = '{ "FirstName": "John", "LastName": "Doe" }'; // GOOD
This means that the JSON used by ASP.NET web services is not portable. To get around this, I created a little JavaScript function that handles one-dimensional associative arrays and converts them to “MS-JSON.” You may want to adapt this function to your needs since it explicitly doesn’t handle “objects” such as arrays or dates:
JavaScript:
function GenerateAspNetJsonString(MyArray) {
var StrOut = '{';
for (var key in MyArray) {
if ('object' != typeof MyArray[key]) {
// The type is not an object, so we can write it down as a string:
StrOut += '"' + key + '":"' + escape(MyArray[key]) + '",'
}
}
// Strip the trailing comma and return:
return StrOut.substr(0, StrOut.length - 1) + '}';
}
#2: Check your Attributes
Chances have that you already marked your web method with the following code:
VB.NET:
<WebMethod(Description:="Does something.")> _ <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _ Public Function MyMethod(ByVal Something as String) As String ...
C#:
[WebMethod(Description = "Does something.")] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string MyMethod(string Something) ...
But make sure that you have also marked the Web Service’s class with the ScriptService (short for ScriptServiceAttribute) attribute as well. This is not automatically done by Visual Studio when the code is generated, so you’ll have to do it manually:
VB.NET:
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _ <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <ScriptService()> _ Public Class MyWebService Inherits System.Web.Services.WebService ...
C#:
[WebService(Namespace = "http://www.williamsportwebdeveloper.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyWebService : System.Web.Services.WebService { ...
#3: When all else fails
Check your XmlHttpRequest’s errors, since ASP.NET actually returns quite a bit of useful information in debug mode. Using jQuery, you can add an error handler to your $.ajax function call:
JavaScript:
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
url: '/MyWebService.asmx/MyWebMethod',
// notice the usage of the function provided in #1:
data: GenerateAspNetJsonString({
FirstName: 'John',
LastName: 'Doe'
}),
dataType: 'json',
error: function (jqXHR, textStatus, errorThrown) {
// responseText contains all of the interesting
// bits and pieces:
alert(jqXHR.responseText);
},
success: function (msg) {
// do something with msg.d, eval() to
// to retrieve the data.
}
});
The next tutorial covering some basic data types and in introduction into variables using the C programming language is now available. Make sure to watch the 1080p version if your connection allows it:
Due to time restrictions, all of the promised future C tutorials will be in video format. Here’s the video reworking of my previous written “Hello, World!” tutorial (make sure to watch it in 1080p if you can):
I hope you learned something, and if you have any comments or questions, please let me know by posting a comment below.
Operations Table
| Operation | Sample | C Operator |
|---|---|---|
| AND | 1001 AND 0101 = 0001 |
& (ampersand) |
| OR | 1001 OR 0101 = 1101 |
| (pipe) |
| NOT | NOT 1001 = 0110 |
~ (tilde) |
| XOR | 1001 XOR 1011 = 0010 |
^ (caret) |
| LEFT-SHIFT | LEFT-SHIFT 0001 BY 1 = 0010LEFT-SHIFT 0001 BY 3 = 1000 |
<< |
| RIGHT-SHIFT | RIGHT-SHIFT 1000 BY 1 = 0100RIGHT-SHIFT 1000 BY 3 = 0001 |
>> |
C Assignment Operators
| Operation | Syntax | Short For |
|---|---|---|
| AND | X &= Y; |
X = X & Y; |
| OR | X |= Y; |
X = X | Y; |
| XOR | X ^= Y; |
X = X ^ Y; |
| LEFT-SHIFT | X <<= Y; |
X = X << Y; |
| RIGHT-SHIFT | X >>= Y; |
X = X >> Y; |
Setting & Checking Bit-Flags:
#include <stdlib.h>
#define FLAG_ONE 0x0001 /* 0001 */
#define FLAG_TWO 0x0002 /* 0010 */
#define FLAG_THREE 0x0004 /* 0100 */
int main(void)
{
unsigned int Flags = 0x0000; /* Initialize to zero */
Flags |= FLAG_ONE; /* Flags now contains 0001 */
Flags |= FLAG_THREE; /* Flags now contains 0101 */
/* will return EXIT_SUCCESS since we never set FLAG_TWO: */
return (Flags & FLAG_TWO) ? EXIT_FAILURE : EXIT_SUCCESS;
}
Counting Set Bits:
#include <stdlib.h>
#include <stdio.h>
#define BITS_PER_INT ( sizeof(unsigned int) * 8 )
int main(void)
{
unsigned int Flags = 0x0115; /* Dec: 277, Bin: 100010101 */
unsigned int Mask = 0x0001;
unsigned int Count = 0;
unsigned int i = 0;
for (i = 0; i < BITS_PER_INT; ++i, Mask <<= 1)
if (Flags & Mask)
++Count;
// The following line should print: Flags contains 4 set bits.
printf("Flags contains %d set bits.\n", Count);
return EXIT_SUCCESS;
}
If you ask a programmer which programming language you should learn as your first, they’ll often prescribe you their personal favorite, often not considering if the language is supported on multiple platforms, easy to learn, exposing underlying system mechanics, and fully featured.
This post is the first of a series of tutorials intended to teach you the C programming language, an excellent first language because of the following reasons:
- Supported on all major operating systems
- Relatively straight-forward syntax
- Mature and stable feature set
- Used in many articles, books, and other publications
- Easy transition into higher-level languages
Okay, so I know you’re probably here because you searched for “window wrapper class” or something similar and expected the article that I use to host on Scriptionary that threw a bunch of C++ code at you for you to copy and paste. I regret to inform you that the article you were looking for has ceased to exist. Sorry about that.
However, in its place I have for you this very post which will teach you how to accomplish creating such a wrapper all by yourself. I hope that is okay since all you really need to know is the how to create a window procedure that you can use with your custom class.
Read the rest of this entry »
This post lists the code for creating a high resolution timer for the Microsoft Windows platform. High resolution timers are often used in multimedia and entertainment applications for timing events up to the microsecond.
This is a heavily modified re-post of the article that used to be on the Scriptionary.com website before the change to the blog, read the source code for details.
Read the rest of this entry »
Scriptionary 
