Chitika

Wednesday, February 13, 2013

linux enable sftp

To enable sftp on linux (having openssh-server installed), one have to uncomment/open following line in /etc/ssh/sshd_config file, and restart sshd server (this line is already enabled in all the distributions by default, means sftp is already working):

Subsystem       sftp    /usr/lib/ssh/sftp-server


To connect to linux sftp, use sftp user@server from command line and use normal ftp (put/get/mput/mget) commands to retrieve and send files to server.

Tuesday, February 12, 2013

C# Cross thread GUI update issue

Following is the pattern to handle C# cross thread GUI update issue (ref: stackoverflow)


delegate void SetTextCallback(string text);

private void SetText(string text)
{
  // InvokeRequired required compares the thread ID of the
  // calling thread to the thread ID of the creating thread.
  // If these threads are different, it returns true.
  if (this.textBox1.InvokeRequired)
  { 
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
  }
  else
  {
    this.textBox1.Text = text;
  }
}

Monday, February 11, 2013

debug c++ activeX control of different project

Sometimes solutions are big, consisting of different project. When we have to debug these projects we get the notification that: Breakpoint will not currently be hit. No symbol loaded for this document.

To debug such project, following are the steps:

  1. Clean solution
  2. Rebuild solution
  3. Run the executable project (by setting it as startup project)
  4. When the said activeX just launched by the executable, go to debug and attach to the executable containing the activeX
  5. now, easily debug the activeX project.

Thursday, February 7, 2013

JNI NDK for android developers (and casual c programmer)

  1. Download NDK
  2. extract NDK on c drive (no space in path)
  3. create jni folder in eclipse android project, put c file(s), make file and libraries (.a/.o) in this folder
  4. c file should be as follows (lowlevelfunctions.c), note signature of function:
#include <string.h>
#include <jni.h>
#include "thirdpartylib.h"


jstring Java_com_example_LowLevelFunctions_getInfo(JNIEnv* env, jobject javaThis) {

return (*env)->NewStringUTF(env, "hello world");
}

/************************************
*** and make file as follows (Android.mk): ***
************************************/


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := lowlevelfunctions
LOCAL_MODULE_TAGS := lowlevelfunctions
LOCAL_SRC_FILES := lowlevelfunctions.c
LOCAL_ARM_MODE := arm

LOCAL_LDFLAGS = $(LOCAL_PATH)/thirdpartylib.a
#LOCAL_STATIC_LIBRARIES := thirdpartylib

include $(BUILD_SHARED_LIBRARY)

5.  on cmd, navigate to jni folder
6. on the jni folder, execute ndk-build executable directly from NDK root path
7. liblowlevelfunctions.so will be created to the parent folder's lib folder
8. to include it in the java, note the function name in c file, it should be added in the class LowLevelFunctions of com.example package, named getInfo, as follows:

package com.example;

class LowLevelFunctions {

static {
System.loadLibrary("lowlevelfunctions"); // as defined in make file LOCAL_MODULE
}

public static native String getInfo();

}

9. If you want to create executable, include int main() function in c file and include $(BUILD_EXECUTABLE) in last line of make file.




Wednesday, February 6, 2013

Editing dot net resource .resx file

.Net Resource Editors are third party programs/freewares, which could be used to edit resource files (having extension .resx).

Following is similar utility:

http://fishcodelib.com/files/ResourceNet2.zip

C# get fully qualified domain name

Use following code snippet to get fully qualified domain name of current machine:



string domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
string hostName = Dns.GetHostName();
string fqdn = "";

if (!hostName.Contains(domainName))
    fqdn = hostName + "." + domainName;
else
    fqdn = hostName;

C# Auto detect default IP Address


Use following code snippet to automatically detect the default IP address:



  IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
                    if (localIPs != null &&
                        localIPs.Length > 0)
                    {
                        tbServerName.Text = (from localIP in localIPs
                                             where localIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
                                             select localIP.ToString()).FirstOrDefault();
                    }
                    else
                    {
                        tbServerName.Text = "";
                    }