Friday, November 27, 2015

C++ DLL Export: Decorated/Mangled names

I got good answser from http://stackoverflow.com/questions/2804893/c-dll-export-decorated-mangled-names for the c++ dll export names.

//---------------------------------------------------------------------------------------------------
// Test cases built using VC2010 - Win32 - Debug / Release << doesn't matter
//---------------------------------------------------------------------------------------------------
// SET: Project > Properties > Linker > Debugging > Generate Debug Info = Yes (/DEBUG)
//  || (or, also doesn't matter)
// SET: Project > Properties > Linker > Debugging > Generate Debug Info = No + delete PDB file!

extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback

extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> _SetCallback@4

__declspec(dllexport) void SetCallback(LPCALLBACK function);
> ?SetCallback@@YAXP6AXHPADPAX@Z@Z

__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> ?SetCallback@@YGXP6GXHPADPAX@Z@Z  

//---------------------------------------------------------------------------------------------------
// this also big is nonsense cause as soon you change your calling convention or add / remove
// extern "C" code won't link anymore.

// doesn't work on other cases
#pragma comment(linker, "/EXPORT:SetCallback")
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);

// doesn't work on other cases
#pragma comment(linker, "/EXPORT:SetCallback=SetCallback")
extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);

// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=_SetCallback@4")
extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);

// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=?SetCallback@@YAXP6AXHPADPAX@Z@Z")
__declspec(dllexport) void SetCallback(LPCALLBACK function);

// doesn't work on other cases / creates alias
#pragma comment(linker, "/EXPORT:SetCallback=?SetCallback@@YGXP6GXHPADPAX@Z@Z")
__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);

//---------------------------------------------------------------------------------------------------
// So far only repetable case is using Module-Definition File (*.def) in all possible cases:
EXPORTS
  SetCallback

extern "C" __declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback

extern "C" __declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> SetCallback

__declspec(dllexport) void SetCallback(LPCALLBACK function);
> SetCallback

__declspec(dllexport) void __stdcall SetCallback(LPCALLBACK function);
> SetCallback

// And by far this is most acceptable as it will reproduce exactly same exported function name
// using most common compilers. Header is dictating calling convention so not much trouble for
// other sw/ppl trying to build Interop or similar.

Wednesday, September 16, 2015

Some or all identity references could not be translated

Asp.net  web app access Active directory.
 SecurityIdentifier sid = new SecurityIdentifier((byte[])searchResultEntry.Attributes[AttObjectSid][0], 0);
NTAccount nt = (NTAccount)sid.Translate(typeof(NTAccount));

Translate throw exception.

The issue is  I use asp pool identity to access Active Directory.

But my develop machine is not joined to the accessed AD.
Most feature are ok.

Friday, June 26, 2015

Firfox install addon success on normal version failure on developer edition

I worked on Opensource project Mitro, try add idQ Trusted sevice authorization to it.
After I made some change on Mitro firefox plugin, this plugin could not install on firefox developer edition, but the normal firefox works.

Finally, I found the issuse was :



14:41:46.304 1435344106304 addons.xpi ERROR Failed to remove file C:\Users\....\AppData\Roaming\Mozilla\Firefox\Profiles\4c3molwx.dev-edition-default\extensions\trash\adbhelper@mozilla.org\win32\adb.exe: [Exception... "Component returned failure code: 0x80520015 (NS_ERROR_FILE_ACCESS_DENIED) [nsIFile.remove]"  nsresult: "0x80520015 (NS_ERROR_FILE_ACCESS_DENIED)"  location: "JS frame :: resource://gre/modules/addons/XPIProvider.jsm :: recursiveRemove :: line 1506"  data: no] Stack trace: recursiveRemove()@resource://gre/modules/addons/XPIProvider.jsm:1506 < forEach()@self-hosted:210 < recursiveRemove()@resource://gre/modules/addons/XPIProvider.jsm:1521 < forEach()@self-hosted:210 < recursiveRemove()@resource://gre/modules/addons/XPIProvider.jsm:1521 < forEach()@self-hosted:210 < recursiveRemove()@resource://gre/modules/addons/XPIProvider.jsm:1521 < DirInstallLocation_getTrashDir()@resource://gre/modules/addons/XPIProvider.jsm:7521 < DirInstallLocation_installAddon()@resource://gre/modules/addons/XPIProvider.jsm:7543 < AI_startInstall/<()@resource://gre/modules/addons/XPIProvider.jsm:5996 < next()@self-hosted:674 < TaskImpl_run()@resource://gre/modules/Task.jsm:330 < Handler.prototype.process()@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:867 < this.PromiseWalker.walkerLoop()@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:746 < this.PromiseWalker.scheduleWalkerLoop/<()@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:688 < <file:unknown>1 Log.jsm:749:0

I delete and try reinstall it.


Tuesday, June 9, 2015

Windows cannot find the Microsoft Software License Terms. Make sure the installation sources are valid and restart the installation.

I install windows 2012 r2 on VMware player 6.3 always get error "Windows cannot find the Microsoft Software License Terms.  Make sure the installation sources are valid and restart the installation."

To fix this issue:
When you create a New virtual machine
- Choose the option: I will install the operating system later.(The virtual machine will be created with a blank hard disk.)
   Do not type the path to the ISO file
- After creating New virtual machine additionally set the path to the ISO file (VM/ settings/ CD-DVD.../Use ISO image file)
- After running the New virtual machine to automatically install classicl (otherwise runs probably Express= error)

Friday, May 15, 2015

Add Listen ports to apache in ubuntu

http://www.cyberciti.biz/faq/linux-apache2-change-default-port-ipbinding/

Question: How do I change Apache 2 default port under Debian / Ubuntu Linux? I've couple of public IPv4 address assigned by my ISP, how do I force Apache2 to listen to a specific IP address? How do I change Apache2 IP address binding?
Answer: You can easily change the port and other settings using following directives:


Task: Change Apache port

Open /etc/apache2/ports.conf file, enter:
# vi /etc/apache2/ports.conf
OR
$ sudo vi /etc/apache2/ports.conf
To make the server accept connections on both port 8010, enter:
Listen 8010
To make the server accept connections on both port 80 and port 8010, use:
Listen 80
Listen 8010

Task: Accept connections on specific IP / interface port

You need to use network interfaces IP address. For example, to make the server accept connections on IP 202.54.1.2 and port 80, enter:
Listen 202.54.1.2:80
To make the server accept connections on two specified interfaces and port numbers, enter:
Listen 202.54.2.1:80
Listen 202.54.2.5:8010

Save and close the file. Restart Apache 2 webserver:
# /etc/init.d/apache2 restart
OR
$ sudo /etc/init.d/apache2 restart

How do I verify port and IP binding working ?

Use netstat command to find out if Apache is listening on a specific port or not, use:
# netstat -tulpn
# netstat -tulpn | grep :80
# netstat -tulpn | grep :8010
# netstat -tulpn| grep 202.54.1.2:80

Sample output:
tcp        0      0 202.54.1.2:80            0.0.0.0:*               LISTEN  


Friday, May 1, 2015

Problem of Thread examples

Example Thread Problems
pthread create - create a new thread
int pthread create(pthread t * thread, pthread attr t * attr, void * (*start routine)(void
*), void * arg);
pthread self - return identifier of current thread
pthread t pthread self(void);
pthread exit - terminate the calling thread
void pthread exit(void *retval);
pthread join - wait for termination of another thread
int pthread join(pthread t th, void **thread return);
pthread detach - put a running thread in the detached state
int pthread detach(pthread t th);
pthread cancel - thread cancellation
int pthread cancel(pthread t thread);
Semaphores
int sem init(sem t *sem, int pshared, unsigned int value);
int sem wait(sem t * sem); /* P */
int sem post(sem t * sem); /* V */
Problem 1:
Consider the following programs which uses threads. Assume that all system calls return normally. Circle
the programs which are guaranteed to print out 42.
Program 1
void *thread(void *vargp)
{
pthread_exit((void*)42);
}
int main()
{
int i;
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, (void **)&i);
printf("%d\n",i);
}
Page 1 of 6
Program 2
void *thread(void *vargp)
{
exit(42);
}
int main()
{
int i;
pthread_t tid;
pthread_create(&tid, NULL, thread, NULL);
pthread_join(tid, (void **)&i);
printf("%d\n",i);
}
Program 3
void *thread(void *vargp)
{
int *ptr = (int*)vargp;
pthread_exit((void*)*ptr);
}
void *thread2(void *vargp)
{
int *ptr = (int*)vargp;
*ptr = 0;
pthread_exit((void*)31);
}
int main()
{
int i = 42;
pthread_t tid, tid2;
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_create(&tid2, NULL, thread2, (void*)&i);
pthread_join(tid, (void**)&i);
pthread_join(tid2, NULL);
printf("%d\n",i);
}
Page 2 of 6
Program 4
void *thread(void *vargp)
{
pthread_detach(pthread_self());
pthread_exit((void*)42);
}
int main()
{
int i = 0;
pthread_t tid;
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_join(tid, (void**)&i);
printf("%d\n",i);
}
Program 5
int i = 42;
void *thread(void *vargp)
{
printf("%d\n",i);
}
void *thread2(void *vargp)
{
i = 31;
}
int main()
{
pthread_t tid, tid2;
pthread_create(&tid2, NULL, thread2, (void*)&i);
pthread_create(&tid, NULL, thread, (void*)&i);
pthread_join(tid, (void**)&i);
pthread_join(tid2, NULL);
}
Page 3 of 6
Problem 2:
This problem tests your understanding of race conditions in concurrent programs.
Consider a simple concurrent program with the following specification: The main thread creates two peer
threads, passing each peer thread a unique integer thread ID (either 0 or 1), and then waits for each thread
to terminate. Each peer thread prints its thread ID and then terminates.
Each of the following programs attempts to implement this specification. However, some are incorrect
because they contain a race on the value of myid that makes it possible for one or more peer threads to print
an incorrect thread ID. Except for the race, each program is otherwise correct.
You are to indicate whether or not each of the following programs contains such a race on the value of
myid.
A. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int myid;
myid = *((int *)vargp);
Free(vargp);
printf("Thread %d\n", myid);
}
int main() {
pthread_t tid[2];
int i, *ptr;
for (i = 0; i < 2; i++) {
ptr = Malloc(sizeof(int));
*ptr = i;
Pthread_create(&tid[i], 0, foo, ptr);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 4 of 6
B. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int id;
id = *((int *)vargp);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
Pthread_create(&tid[i], NULL, foo, &i);
Pthread_join(tid[0], NULL);
Pthread_join(tid[1], NULL);
}
C. Does the following program contain a race on the value of myid? Yes No
void *foo(void *vargp) {
int id;
id = (int)vargp;
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
Pthread_create(&tid[i], 0, foo, i);
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 5 of 6
D. Does the following program contain a race on the value of myid? Yes No
sem_t s; /* semaphore s */
void *foo(void *vargp) {
int id;
id = *((int *)vargp);
V(&s);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
sem_init(&s, 0, 0); /* S=0 INITIALLY */
for (i = 0; i < 2; i++) {
Pthread_create(&tid[i], 0, foo, &i);
P(&s);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
E. Does the following program contain a race on the value of myid? Yes No
sem_t s; /* semaphore s */
void *foo(void *vargp) {
int id;
P(&s);
id = *((int *)vargp);
V(&s);
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
sem_init(&s, 0, 1); /* S=1 INITIALLY */
for (i = 0; i < 2; i++) {
Pthread_create(&tid[i], 0, foo, &i);
}
Pthread_join(tid[0], 0);
Pthread_join(tid[1], 0);
}
Page 6 of 6

Monday, March 9, 2015

Autoconf does not work

I have a sample project, call

autoconf
./configure
sudo make  install

worked profectly.

I copy every thing, and create my own project.

meet some issues. autoconf not work properly.

Here are the issues I met:

1:  undefined macro am_init_automake.

run aclocal  first , fix this problme


2:  ./configuration missing  makefile.in.

the makefile.in did not generate properly.

I compare everything with sample project and mine.  finally,  I found there are two files, I did not create correctly.

1 NEWS   file is missing , even it is a empty file.
2 README  file is missing, I put it in parent folder.

When I create these 2 files.

3:  configure.ac
Makefile.am
end of line:
\r\n  should be \n

The third one is important.


automake work's fine.

but it should run script as  below:

aclocal
autoconf
autoreconf -vif
./configure
sudo make  install

Tuesday, March 3, 2015

How to properly malloc for array of struct in C

What's the difference between

struct mystruct *ptr = (struct test *)malloc(n*sizeof(struct test));
and

struct mystruct **ptr = (struct test *)malloc(n*sizeof(struct test *));
They both work fine, I'm just curious about the actual difference between the two. Does the first one allocate an array of structs, whereas the second one an array of struct pointers? The other way around? Also, which one has a smaller memory footprint?


The first allocates an array of struct, and the other allocates an array of pointers to struct. In the first case, you can write to fields by assigning ptr[0].field1 = value; right away, while in the second case you must allocate the struct itself before doing the actual writing.

It is OK to drop the cast of malloc result in C, so you could write

struct mystruct **ptr = malloc(n*sizeof(struct test *));
for (int i = 0; i != n ; i++) {
    ptr[i] = malloc(sizeof(struct test));
}
ptr[0]->field1 = value;
...
// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(ptr[i]);
}
free(ptr);

Thursday, February 26, 2015

Linux script not run

I have a script to run on linux.
This is the first time I try.

I upload the script from windows to linux. (ftp)

run the script ./test

got the error :   Permission Denied.

then I just think run as root.

so I do  :  sudo ./test

got the error:  command not found.

to fix it. only need to do is:

chmod +x test
./test  is running !!

Thursday, January 22, 2015

test msi package

create log to analysis the msi error.
msiexec /l*vx install.log /i mypackage.msi
msiexec /l*vx install.log /x mypackage.msi

Friday, January 16, 2015

Could not change windows account password

There is a account policy  Minimum password age  default 1 days.

Show If you just change the password, it will fail. just told you  history, length ....


Thursday, January 15, 2015

http://www.paralint.com/blog/2011/03/26/how-to-debug-a-credential-provider-locally/

http://www.paralint.com/blog/2011/03/26/how-to-debug-a-credential-provider-locally/

How to debug a Credential Provider locally

Here is a quick and easy way to debug a Credential Provider running on your development machine, without needing to set up a kernel debugging session with two computers. Before you go down this road, let me tell you a little bit about LogonUI.exe behavior (as seen on Windows 7 ultimate SP1 64 bits) set to require CTRL-ALT-DEL to log on.
  • Every time you type CTRL-ATL-DEL, a new LogonUI process is launched.
  • LogonUI will try to load any registered Credential Providers COM objects.
  • You can run any process on the secure desktop
With that knowledge, it is easy to set up a debugging session for your Credential Provider, right on your development machine. Before I continue, be aware that it might affect the stability of your computer temporarily, as the following illustration shows.
6015610
To debug your credential provider, you will need this :
I guess you could also use Visual Studio instead, ymmv. If it works, please drop us a line in the comments.
To start debugging, here is what you have to do:
  1. Start a command shell on the Secure Desktop with this command:
    psexec -dsx cmd.exe
  2. Build a debug version of your Credential Provider and register it.
  3. Type CTRL-ALT-DEL to switch to the secure desktop. That will also launch LogonUI.exe
  4. Hit Alt-Tab to switch to the command prompt you started at step 1
  5. Change to the directory where your source code is
  6. Debug LogonUI.exe and set the source path at once, with this command :
    windbg –pn logonui.exe –srcpath %CD%
That’s it. You might not be very familiar with windbg, so here are a few tips to get you started:
  • When you attached to LogonUI at step 6, the process is stopped. You can enter commands to set breakpoints before resuming it.
  • Verify that your Credential Provider is loaded with the lm command. Look for a string like this one:
    000007fe`f5e80000 000007fe`f5e9b000   SampleCredentialProvider   (private pdb symbols)
  • Just to be sure, verify that a specific symbol is loaded with this command (you should get an address):
    x SampleCredentialProvider!CSampleProvider::SetUsageScenario
  • Set a breakpoint to the same location with this command:
    bu SampleCredentialProvider!CSampleProvider::SetUsageScenario
  • Resume LogonUI with the g command.
You can now lock your workstation and try to unlock it. When LogonUI appears to freeze, ATL-TAB to the debugger. It should be waiting for you with the source file loaded, waiting for your instructions. Type g to resume. Complete the unlock procedure to end LogonUI.
To reattach to logon UI, you can quit windbg and launch it again, but it is easier to list the process with the .tlist command (LogonUI.exe will be the last in the list). Attach to it again with .attach 0n2331 (replace with your PID).
Happy debugging !